Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views

JAVA Unit-1

Java is an object-oriented programming language that has several key features and advantages. It is platform independent, meaning that code written in Java can run on any device without modification. Java code is compiled to bytecode, which can be run on any Java Virtual Machine (JVM) regardless of the underlying hardware or operating system. The JVM handles running the bytecode and converting it to native machine code as needed. This makes Java portable and able to run on any system with a JVM. Java is also secure because of features like sandboxing and absence of pointers that help prevent security vulnerabilities.

Uploaded by

Vraj Patel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

JAVA Unit-1

Java is an object-oriented programming language that has several key features and advantages. It is platform independent, meaning that code written in Java can run on any device without modification. Java code is compiled to bytecode, which can be run on any Java Virtual Machine (JVM) regardless of the underlying hardware or operating system. The JVM handles running the bytecode and converting it to native machine code as needed. This makes Java portable and able to run on any system with a JVM. Java is also secure because of features like sandboxing and absence of pointers that help prevent security vulnerabilities.

Uploaded by

Vraj Patel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Unit-1

Introduction To Java
Features/Importance/Advantages of Java Programming
Que:
1. Explain the three advantages of java.
2. List any four features of Java.
3. List out Java features and explain any two of them.
4. List different features of Java. Explain any one of it.
5. How Java is strongly associated with the Internet? (Secure and Portable)
6. How Java is secure & portable than other language.
7. Explain following
Portable (ii)Platform Independent
Ans:
 The Features of Java Programming are as below:
1. Object Oriented :
 In java everything is an Object.
 Object Model in Java is easy to extend.
 It supports OOPS features such as class, objects, encapsulation, Abstraction,
inheritance, polymorphism etc.
2. Platform independent (Architectural-Neutral):
 Java is platform independent because Java code can be run on any machine with any
processor or any operating system i.e. Write Once,Run anywhere,any time,forever.
 When Java code is compiled by the compiler, it is converted into bytecode. This
bytecode is a platform independent code. This byte code is distributed over the web
and interpreted by Java virtual Machine (JVM) on whichever platform it is being run.
3. Simple :
 Java is designed to be easy to learn and understand.
 Its syntax is simple and inherited from C and C++.
 If you understand the basic concept of OOP learning java will require very little
efforts.
 Some of confusing concepts of C++ are removed from Java e.g. Explicit Pointer,
Operator Overloading etc. No need to delete an object to free memory instead of that
Java supports automatic garbage collection feature.
4. Secure :
 Java is secured because it does not support explicit pointer.
 Java also provides firewall between networked application and your computer.
 When we use a Java-Compatible Web browser, we can safely download any file
without fear of viral infection or malicious intent.
5. Portable :
 Compiler and Java are written in ANSI C with a portability feature.
 Being architectural neutral and having no dependent aspects of the specification
makes Java portable.
6. Robust :
 Robust means strong. Java is robust.
 Thera are lack of pointer that avoids security problems.

Page 1 of 10
 Java Provide automatic management memory using garbage collection and supports
exception handling, so programmer can manage run-time errors also.
 Java is a strictly typed language. It checks code at compile time as well as at run time.
 All these features make a Java Robust.
7. Multi-threaded :
 With Java‟s multi-threaded feature it is possible to write programs that can do many
tasks at the same time.
8. Interpreted :
 Java Provides cross-platform by compiling Java code into Java byte code. This code
can be interpreted on any system by JVM (Java Virtual Machine).
9. High Performance:
 Java is designed to perform well on very low-power CPUs.
 Though Java was designed for interpretation, it is carefully designed so that it would
be easy to translate directly into native machine code for very high performance by
using a just-in-time compiler (JIT).
10. Distributed :
 Java is designed for the distributed environment of the internet.
 Java allows objects on two different computers to execute procedures remotely by
using RMI (Remote Method Invocation) package.
 RMI is used for creating distributed environment (Client-Server Programming). So,
we can access any file by calling the method from any computer remotely.
11. Dynamic :
 Java supports dynamic loading of classes. It means classes are loaded on demand.
 Java supports dynamic compilation and automatic memory management.
 It is designed to adapt to an evolving environment.

Byte Code and Virtual Machine

Que:
1. Describe Byte code.
2. Define byte code and source code.
3. Define „bytecode‟ in Java.
4. State the importance of byte code.
5. Explain following
(i) Byte Code (ii) JVM (iii) Platform Independent
6. Describe JVM in Java.
7. Explain JVM.
Ans:
ByteCode
 Byte Code is intermediate code generated by Java Compiler.
 When Java code is compiled by the Java compiler, it produces bytecode not executable
code.
 Bytecode is a highly optimized set of instructions designed to be executed by the Java
run–time system, which is called as Java Virtual Machine(JVM).
 The JVM is an interpreter for bytecode.
 Bytecode is a platform independent code. Hence, translating a Java program into
bytecode makes it much easier to run a program in a wide variety of environments.

Page 2 of 10
Java Virtual Machine (JVM)
 JVM provides runtime environment to execute java byte code.
 It converts Byte Code into native Code.
 Byte code is platform independent while JVM is platform Dependent. Hence, Byte code
remains same for all platforms, only JVM needs to be implemented for each platform.
 Once the JVM exits for a given system, any Java program can run on it.
 The details of the JVM will differ from platform to platform; all JVM can interpret the
same Java bytecode.
 Java Program is executed by the JVM. So it helps to solve the major problems associated
with downloading programs over the Internet.
 As the Java program is interpreted also helps to make it secure. Because the execution of
every Java program is under the control of the JVM.
 JIT (Just in time Compiler) of JVM gives very high performance by directly translating
byte code into native code.
 JVM Performs following operations:
o Loads Code
o Verifies Code
o Executes Code
o Provides run-time Environment

Just-in-Time Compiler
 Although Java was designed as an interpreted language, there is nothing about java that
prevents on-the-fly compilation of byte code into native code in order to boost
performance.
 Sun supplies its Just-in-Time (JIT) compiler for bytecode.
 When JIT Compiler is part of JVM, selected portions of bytecode are compiled into
executable code in real time, on a piece-by-piece, demand basis.
 It is not possible to compile entire java program into executable code all at once, because
java performs various run time checks that can be done only at run time.
 But JIT Compiler Compiles code as it is needed, during execution.
 JIT approach still boost significant performance.

Page 3 of 10
Features of Object Oriented Language
Que:
1. Explain Java‟s Object oriented feature.
2. List out OOP concepts.
3. List four different OOP concepts.
4. List the different OOP concepts.
5. List basic concepts of OOP. Explain any one.
Ans:

Object
 An object represents an instance of a class.
 Objects are the basic run-time entities in an object-oriented system
 There can be more than one instance of an object.
 Each instance of an object can hold its own relevant data.
Class
 Classes has the data and its associated function wrapped in it.
 In the OOPs concept the variables declared inside a class are known as "Data Members"
and the functions are known as "Member Functions"
Inheritance
 Inheritance is the process by which object of one class acquire the properties and
methods of another class.
 It provides code reusability.
Polymorphism
 In polymorphism, an operator and function may shows different behavior in different
instances.
 It means single name can be used for different purpose.
 For Ex- Operation of addition for two numbers, will generate a sum. If the operands are
strings, then the operation would produce a third string by concatenation.
 Java allows method overloading and operator overloading.
Abstraction
 Abstraction refers to the act of representing essential features without including the
background details.
 For example: Driving Car, we don't know the internal parts of car.
Encapsulation
 Binding (or wrapping) code and data together into a single unit is known as
encapsulation. For example: capsule, it is wrapped with different medicines.
 A java class is the example of encapsulation.
 Java bean is the fully encapsulated class because all the data members are private here.

Page 4 of 10
Difference between POP and OOP

Que:
1. State the difference between Procedure v/s Objected Oriented Language.
2. Write any two difference between Java and C.
Ans:

Procedure Oriented Programming (C) Object Oriented Programming (Java)


1 In POP, program is divided into small parts In OOP, program is divided into class and
called functions. objects.
2 Main focus is on the function not on data. Main focus is on the data rather than
function.
3 POP follows Top Down approach. OOP follows Bottom Up approach.
4 Data move openly around the system from Data is hidden and can not be accessed by
function to function. external functions.
5 POP does not have any proper way for OOP provides Data Hiding so
hiding data so it is less secure. provides more security.
6 To add new data and function in POP is not OOP provides an easy way to add new data
so easy. and function.
7 POP does not have access specifier. OOP has access specifiers named private,
public and protected etc.
8 POP does not support Overloading feature. OOP supports function overloading and
operator overloading.
9 Example : C, VB, FORTRAN, Pascal. Example : C++, JAVA, VB.NET

Difference between C++ and Java


Que:
1. Write any four differences between Java and C++.
Ans:
C++ Java
1 C++ support structure, union, template, Java doesn‟t support all these features. Java
preprocessor, default arguments, operator has concept of “restricted pointers” that uses
overloading and pointers. references which acts like pointers. But we
can‟t perform arithmetic operations on it.
2 C++ support destructor, which is called to Java doesn‟t support destructor because it
destroy the objects. supports automatic garbage collection.
3 In C++ we can declare global variables and Java doesn‟t have scope resolution operator
can define methods outside the class using and we can declare global variables. The
scope resolution operator (::). methods can only be defined inside the class.

Page 5 of 10
4 C++ support goto statement. Use of goto is goto and const keywords are reserved in Java
not considered good because it makes but they are not used.
difficult to understand the program.
5 C++ supports multiple inheritance and it Java doesn‟t support multiple inheritance.
can be implemented using class. Although it can be implemented using
interface.
6 C++ is a platform dependent language. Java is a platform independent language. Write
Write once, compile anywhere (WOCA). once, run anywhere/everywhere (WORA /
WORE).
7 C++ only uses compiler. Java uses both interpreter and compiler.

8 C++ doesn‟t have built in thread support. Java has built in thread support. There is a
Thread class which is used to implement
multithreading.

The Basic Structure of a Simple Java Program


Que:
1. Describe the Structure of a typical Java program.
2. Explain the structure of Java program with example.
3. Explain basic structure of Java programs.
4. Draw and explain Java program structure.
5. Explain main method of Java program.
6. What is the task of main method in Java program?
7. Describe main() method in Java.
8. Explain the statement: public static void main (String args[]).
Ans:
 A program in Java is a set of class declarations.
 A class contains the declarations of variables and methods that use the variables. Method
contains variable and statements.
 You can use any editor like notepad or any Java IDE for writing java programs.
 Save this file as Welcome.java because in Java Programming Language file name is
same as class name.
 Let us see the simple java program.

class Welcome

// A java program will start from here.

public static void main(String args[ ])

Page 6 of 10
{

System.out.println(" Welcome to Java-Sample!!! ");

Looking into the program line by line

/* This is a sample java program

Save this file as Welcome.java

*/ This is called comment.

 The contents of a comment will be ignored by the compiler. Actually java supports
single line and multiline line comments. The above one is called multi-line comment
which may contain several lines.
 The next line of the code in a program is

class Welcome

{ }

 The word class is a keyword to define a new class and Welcome is a name of the
class. The class definition must begins with opening curly brace ({) and ends with
closing curly brace (}). The rest of the things defined inside these braces are called
member of the class.
 The next line of the code in a program is

public static void main(String args[ ])

 This line begins with main method as like in other languages. The program will start
execute by calling this main method.
 Public Keyword is an access specifier. main must be declared as public,since it must
be called by code outside of its class when the program is started.
 The static keyword allows main() to be called without creating object of the class.
 The keyword void means that the method main( ) does not return any value.
 If we want to pass any information to a method will be received by the variables
declared within the parenthesis is called parameters.

Page 7 of 10
 In a main( ) method there is only one parameter, String args[ ]. args[ ] is a name of
the parameter that is an array of the objects of data type String. args receives any
command line arguments present when the program is executed.
 All the method in java must be start with opening curly brace ({) and ends with
closing curly brace (}).
 The next line of the program is

System.out.println(" Welcome to Java-Sample!!! ");

 The System.out.println helps to display the output in the command line. Here
System is a predefined class that provides access to the system and out is the output
stream that is connected to the console.
 All the statement in java must ends with a semicolon(;).
 And remember that java is case sensitive. So we should be very careful about cases
while coding the program. Otherwise it will lead to the serious problems.

Compiling and Executing the program

Que:
1. Write syntax to compile and execute Java program.
2. Write only compile command and run command of test.java file.
3. Write only compile command and run command of test.java file.
Ans:

 After we have written our program we need to compile and run the program.
 For that we need to use the compiler called javac which is provided by java.
 Go to the command prompt and type the file name as shown here.

c:\> javac Welcome.java

 The javac compiler will create a class file called Welcome.class that contains only
bytecodes.
 These bytecodes have to be interpreted by a Java Virtual Machine (JVM) that will
convert the bytecodes into machine codes.
 Once we successfully compiled the program, we need to run the program in order to
get the output.
 So this can be done by the java interpreter called java. In the command line type as
shown here.

c:\> java Welcome

Page 8 of 10
 So the output will be displayed as

Welcome to Java-Sample!!!

 As we had seen above, when the source code has been compiled, it creates a class
file with .class extension.
 Since this class file contains the bytecodes that can be interpreted by the JVM which
can be resided at any platform.
 Remember that while running the program we are using only .class file but not the
.java file.
 So once you got the class file you can run the same java program at any platform
instead of writing the program again and again.
 This is the very special feature about java that 'Write once and Run anywhere'

Applet and Application


Que:
1. Describe Applets.
2. Define Applet.
Ans:

 Java can be used to create two types of programs: Applications and Applets.
 Java application is a program that runs on your computer, under the operating system of
that computer.
 It is an application that is same as one created in other language.
 Applet is an application designed to be transmitted over the internet and executed by a
Java-Compatible Web Browser. An Applet is actually a tiny Java Program, Dynamically
downloaded across the network, just like an image, sound file or video clip.
 The important difference is that an applet is an intelligent program not just an animation
or media file.

Page 9 of 10
Page 10 of 10

You might also like