Nelson Java Notes Unit1
Nelson Java Notes Unit1
1. Originally Java designed for small, embedded systems like set-top boxes, televisions.
2. First, Java was called "Green talk" by James Gosling and file extension was .gt
3. After that, it was called Oak.
4. In 1995, Oak was renamed as "Java" .
The latest release of the Java Standard Edition is Java SE 8.
Simple
Complex concepts (like pointers, multiple inheritance) are removed from java language. So Java
language is easy to learn and user.
1
Platform Independent
Java Compiler, converts the Java program into bytecode.
This bytecode convert into Machine code / Binary code by Java Interpreter.
This bytecode is a platform independent code and run on any platforms like Windows,
Linux, Unix. i.e. Write Once and Run Anywhere(WORA).
Portable
Java programs can be easily moved from one computers system to another, anywhere and
anytime. Changes and upgrades in operating system resources will not force any changes in java
programs.
Object - Oriented:
Java supports object oriented model.
So, Java supports all the features of object oriented model like:
I. Object
II. Class
III. Inheritance
IV. Polymorphism
V. Abstraction
VI. Encapsulation
Secure:
Java provides data security through encapsulation.
Applet Program provides internet security.
Security Manager tool in Java Virtual Machine (JVM) provides the. Java System Security.
Robust
Java is a Robust language and it provides the following features.
Type checking : Java checks the Data Types in Compile Time and Run Time.
Garbage Collection: It is used to free the unused object's memory automatically.
Exception Handling : It is used to handle the errors during the program execution.
2
Distributed
Java is designed as a distributed language.
It is used to create the applications on networks (Network applications).
Remote Method Invocation (RMI) concept is used to run the Distributed Java
Applications.
High Performance:
Java processing speed is high compare to C and C++.
Java architecture helps to the reduce overheads problem during runtime.
3
Java does not support the C modifiers auto, extern, register, signed, and
unsigned.
Java does not have a preprocessors.
Java adds new operators such as instanceof and >>>.
Java does not support the sizeof, and typedef.
Java has many features required for object-oriented programming.
Java is a true object-oriented language while C++ is basically C with object-oriented extension.
1. Java does not support the operator overloading and Pointers.
2. Java does not support global variables.
3. Java does not use template classes concepts as in C++.
4. Java has replaced the Destructor function with a finalize() function.
5. C++ supports Multiple inheritances . But Java does not support multiple inheritances
of classes. This Multiple inheritance concept is replaced by “Interface” in java.
1. Application:
Applications are executable programs that are controlled by operating system. These applications
have an access to the system resources, such as file systems and can read and write to files on
local computers.
2. Applets
Applets are small executable programs that run on a web page.
Internet users can use Java to create applet programs and run them locally using a Java-
enabled browser such as hot Java, Internet Explorer or Google Chrome or Firefox.
4
Internet user can download the Applets from the Remote Server.
Applets have limited access to system resources.
Application Applet
Application can access the local file system Applet programs has Restricted to access the
and resources. local file system and resources.
Functionality of the applications is very well Functionality of the applications is not known.
known.
Author of the application is known. Author of the application is not known.
Creating and running an application is easy. Creating and running an application is complex
What is Java?
Java is a general-purpose Object Oriented programming language.
Java is concurrent, class-based, object-oriented, and specifically designed to have as few
implementation dependencies as possible.
WORA: "write once, run anywhere" , ie. compiled Java code can run on all platforms
that support Java without the need for recompilation.
5
Java Environment contains Java Development Kit (JDK ) and hundreds of classes ( in-
built or user defined) and methods.
These classes and methods called as Java Standard library (JSL). Also, JSL is known
as API (Application Programming Interface ).
Java Development Kit (JDK) is a collection of tools to develop and run the Java program. JDK
Contains the following tools Java compiler, Javadoc, Jar, and a debugger.
Tools of JDK:
Javac : Java compiler converts the source code into byte code.
Java : Java interpreter converts the bytecode into Machine Code.
Jbd (Java debbuger) - Java debugger helps to debug the errors in Java programs.
Appletviewer – It is used to run the Applet Programs.
Java Application Launcher : It is used to launch the Java applications.
Javap : Java disassembler converts the byte code into program description.
Javah (Java Header File Generator) - Create header files when native method are used.
Javadoc – Create HTML documents from java source code.
6
These collection of classes is called as Java Standard Library (JSL).
The other name of Java Standard Library (JSL) is Java API (Application
Programming Interface)
It physically exists.
7
Hardware Requirement for Java
Hardware Requirements:
• System: Pentium IV 2.4 GHz.
• Hard Disk `: 40 GB.
• Monitor: 15 VGA Color.
• RAM: 256 Mb.
Software
class is a keyword used to decalare class name. All class name should start with the Capital
Letter. Welcome is the class of the above program. Class name and java file name should be
same.
public static void main
The word public is a Access specifier (public, private, protected).
The word static is used to call the main method, before creating any class objects.
The word void indicates that a method does not return a value.
The word main is a method; this is a starting point of a Java program.
8
String[] args
Java main method accepts a single argument of type String array. This is also called as java
command line arguments.
Example for String[] args
public class Args
{
public static void main(String[] args)
{
for(String s : args)
{
System.out.println(s);
}
}
}
Passing Numbers:
Passing String:
The java command-line argument is an argument i.e. passed at the time of running the java
program.
9
class CommandLineExample
{
Output:
1. Reserved Keywords
2. Identifiers
3. Literals
4. Operators
5. Separators
1.Reserved Keywords
Identifiers
Identifiers are used for naming the variables, methods, classes, packages and interfaces.
1. Should not begin with the digit.
2. Have alphabets, digits, underscore, and dollar sign
3. Case Sensitive (Upper & Lower case)
10
Figure : List of Keywords
Literal
A literal represents the constant value. It can be assigned for the variable.
i. Integral literals
ii. Floating
iii. Char literals
iv. .String literals
v. .Boolean literals
Integral literals
Integral literals are used to assign the integer values such as int, long, short and byte
Example:
int x=10;
short x=10;
Floating literals:
Floating literals used to assign the float values
Example:
11
float f=101.222;
Boolean literals
Boolean literals are used to assign the boolean values (true/false)
Example:
boolean b = true;
boolean c = false;
Character Literals
“Char” literal is used to assign the characters within single quotes.
Example:
Char ch = ‘a’;
Char ch = ‘face’;
String literals :
A sequence of characters within double quotes is considered as a string literal.
Example:
Strings="How are you";
Operators
An operator is a symbol that performs an operation. An operator acts on variables
called operands.
Arithmetic operators: These operators are used to perform Arithmetic operations like
12
addition, subtraction, multiplication etc.
Example :
int c = a + b;
float pi = 22 / 7;
·
Unary operators (++, --):
Unary operators: unary operator’s act only on one operand.
Relational operators
Relational operators are used to compare the two values.
Operator Meaning Example
Equa
== l x == 3
13
!= Not equal x != 3
< Less than x<3
> Greater than x>3
<= Less than or equal to x <= 3
Example:
If(a==b)
Logical operators
Logical operators: Logical operators are used to construct compound conditions ie. Logical
operators are used to determine the logic between variables or values:
Operator Meaning Example Explanation
&& AND if(a>b && a>c) If a value is greater than b and c then
Operator only yes is displayed
System.out.print(“yes”);
|| operator OR Operator if(a==1 || b==1) If either a value is 1 or b value is 1 then
yes is displayed
System.out.print(“yes”);
Bitwise operators
These operators act on individual bits (0 and 1) of the operands. They act only on integer data types, i.e. byte,
short, long and int.
Operator Meaning Explanation
& Bitwise AND Multiplies the individual bits of operands
| Bitwise OR Adds the individual bits of operands
^ Bitwise XOR Performs Exclusive OR operation
<< Left shift Shifts the bits of the number towards left a specified
number of positions
>> Right shift Shifts the bits of the number towards right a
specified number of positions sign it.
Example
person instanceof student
14
It will return true if the object person is belong to the class student.
Example:
Person1. age ; (Referece to the variable age)
Person1.salary (Referece to the Method salary)
The java comments are statements that are not executed by the compiler and Java
interpreter.
The comments statement used to provide information about the variable, method or
functions etc..
There are 3 types of comments in java.
Syntax:
//This is single line comment
*/
15
The documentation comment is used to create documentation API. To create
documentation API, you need to use javadoc tool.
Syntax:
/**
This is
documentation
comment
*/
Example:
/** Java Documentation Comment : The Calculator class used to add the given two
2 numbers and multiply the given two . numbers*/
public class Calculator
{
public static void main(String[] args)
{
int a,b,add,mul;
a=100;
b=50;
add=a+b;
mul=a*b;
}
}
16
Used to contains the values of automatically initialized arrays. Also used to define a block
{} Braces
of code, for classes, methods, and local scopes.
[] Brackets Used to declare array types. Also used when dereferencing array values.
Used to separate packages names from subpackages and classes. Also used to separate a
. Period
variable or method from a reference variable.
17