You are on page 1of 23

Core JAVA Page 2

Core JAVA Page 3


History of Java:

Differences between C, C++ and Java:

 C program generates .exe file


 .exe file will be generated by the C compiler according to the rules and
regulations of the platform.

Core JAVA Page 4


Hence, an .exe file generated in one platform can’t be executed in another
platform. So C is platform dependent.

The .exe file generated by C compiler according to one platform can’t be executed
in different platform. So, C is platform dependent.

Core JAVA Page 5


Java:

Byte code: Partially English, partially machine code. (or) Intermediate code.

JVM: JVM is a set of programs, which will automatically present in system when we install
java soft. It is written in ‘C’ language.

JVM internally contains a program called “Interpreter” used to convert the byte code to
machine code. Every platform has its own JVM. So, every JVM converts byte code to
the platform convenient.

 Java compiler generates .class file


 .class file contains byte code instructions (Intermediate code)
 A .class file generated in one computer can be executed in any ther computer
irrespective of platform.
Hence, Java is platform independent

JVM (Java Virtual Machine):

 It is a set of programs written in C language.


 JVM internally consists of an interpreter which takes byte code nstructions and
generates machine code instructions, which can be executed by the
processor.
 JVM is platform dependent

Core JAVA Page 6


Differences between .exe and .class files:

Features of Java:

According to Sun Microsystems, Java is “Simple, Object Oriented, Compiled and Interpreted,
Secured, Robust, Platform Independent, Garbage collected, Multithreaded, Dynamic,
Extensible, High performance”.
1. Simple :
Java is Simple. Since, all the complicated concepts from C++ had been removed.

2. Object Oriented:
Java is Purely Object Oriented programming language. Since, it upports all the features of
Object Oriented Approach such as
 Class/Object
 Encapsulation
 Data Abstraction
 Polymorphism
 Inheritance
3. Compiled and Interpreted
 Java Compiler takes the .java file, and generated .class file.
 .class file contains byte code (Intermediate code)
 The byte code instructions will be taken by the interpreter presence In JVM and
translates to machine code line by line which is executed by the processor. Hence,
Java is both compiled and interpreted language.

Core JAVA Page 7


4. Platform independent:
A .class file générâtes in one Platform can be executed in any other computer
irrespective of platform. Hence, Java is platform independent. But JVM is
platform dependent.

5. Secured:
Since the Java compiler generates .class files, .class files are plaintext files and can’t be affected
by virus.
Hence, Java is secured.
6. Robust:
Java programs are robust programs.
Robust programs are the programs which doesn’t cost any harm to the computer i.e., software
or hardware.
Reason: Exception handling.

Robustness of Java programs can be achieved by the concept of Exception handling.


7. Garbage collector:
Garbage collector is a program that collects (deletes) unwanted data from memory. By that, the
memory can be used in an efficient manner.
8. Multithreaded:
 Multi tasking: Execution of 2 or more programs simultaneously is called as multi tasking.
 Thread: Thread is a part of a program; a thread is also called as light weight process.
Every thread has its own way of execution.
Executing 2 or more threads of a program simultaneously is called as multi- threading.
The main aim of the multi-threading is to reduce the CPU execution time By that the
performance will be improved.

9. Extensible:
The functions related to C and C++ both are the “Native methods” in Java. Java is extended to C
and C++, i.e., we can use the native methods in Java.

10. Dynamic:
In Java always memory allocated dynamically, i.e., the memory will be allocated at run time.
Since the memory is allocated at run time,

 There is no wastage of memory.


 There is no limitation of memory.
11. High performance: JIT compiler (Just In Time) improves the performance of the Java
applications.

Core JAVA Page 8


Versions of
Java:
Java 1.0,
Java 1.1… Java1.7
And the Most of the people are using 1.3 and 1.5 versions.
JDK:
It is a set of tools and utilities that are used to develop Java applications

Core JAVA Page 9


Sample Java Program
Let us look at a simple java program.
class Hello
{
public static void main(String[] args)
{
System.out.println ("Hello World program");
}
}
class : class keyword is used to declare classes in Java
public : It is an access specifier. Public means this function is visible to all.
static : static is again a keyword used to make a function static. To execute a static function you do
not have to create an Object of the class. The main() method here is called by JVM, without creating
any object for class.
void : It is the return type, meaning this function will not return anything.
main : main() method is the most important method in a Java program. This is the method which is
executed, hence all the logic must be inside the main() method. If a java class is not having a main()
method, it causes compilation error.
String[] args : This represents an array whose type is String and name is args. We will discuss more
about array in Java Array section.
System.out.println : This is used to print anything on the console like printf in C language.

Steps to Compile and Run your first Java program


Step 1: Open a text editor and write the code as above.
Step 2: Save the file as Hello.java
Step 3: Open command prompt and go to the directory where you saved your first java program
assuming it is saved in C:\
Step 4: Type javac Hello.java and press Return(Enter KEY) to compile your code. This command
will call the Java Compiler asking it to compile the specified file. If there are no errors in the code the
command prompt will take you to the next line.
Step 5: Now type java Hello on command prompt to run your program.
Step 6: You will be able to see Hello world program printed on your command prompt.

What happens at compile time?


t compile time, java file is compiled by Java Compiler (It does not interact with OS) and
converts the java code into bytecode.

java code com piler byte code

Core JAVA Page 10


 Classloader: is the subsystem of JVM that is used to load class files.
 Bytecode Verifier: checks the code fragments for illegal code that can violate
access right to objects.
 Interpreter: read bytecode stream then execute the instructions.

Core JAVA Page 11


How to set path in Java
For setting the permanent path of JDK, you need to follow these steps:

Copy the path on C Drive Program Files  java  jdl bin

Right click on the My computer Icon

Click on Properties option and click on Advanced System


Settings option

Click on Environment variables option

Core JAVA Page 12


Click on New option and Type Variable Name and Value and click on Ok

VM (Java Virtual Machine)


1. Java Virtual Machine

2. Internal Architecture of JVM

JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides


runtime environment in which java bytecode can be executed.

JVMs are available for many hardware and software platforms (i.e. JVM is platform
dependent).

Core JAVA Page 13


1. A specification where working of Java Virtual Machine is specified. But
implementation provider is independent to choose the algorithm. Its
implementation has been provided by Sun and other companies.
2. An implementation Its implementation is known as JRE (Java Runtime Environment).
3. Runtime Instance Whenever you write java command on the command prompt to
run the java class, an instance of JVM is created.
The JVM performs following operation:
 Loads code
 Verifies code
 Executes code
 Provides runtime environment

Variables and Data Types in Java

Variable
Variable is name of reserved area allocated in memory. In other words, it is a name of
memory location. It is a combination of "vary + able" that means its value can be changed.
Ex: int data=50;//Here data is variable
Types of Variable
There are three types of variables in java:
 local variable
 instance variable
 static variable
Local Variable
A variable declared inside the method is called local variable.

2) Instance Variable
A variable declared inside the class but outside the method, is called instance variable . It is
not declared as static.
3) Static variable
A variable which is declared as static is called static variable. It cannot be local.

Core JAVA Page 14


Data Types in Java:

Boolean Boolean
Data Types

Character char
Premitive
byte
Non Premitive
Numeric short
integer
int

Integral long

float
Floating Point
double

Core JAVA Page 2


Data Types in Java
Java language has a rich implementation of data types. Data types specify size and the type
of values that can be stored in an identifier.
In java, data types are classified into two categories:

 Primitive Data type


 Non-Primitive Data type

Primitive Data type

A primitive data type can be of eight types. Once a primitive data type has been declared its
type can never change, although in most cases its value can change. These eight primitive
type can be put into four groups
Integer: This group includes byte, short, int, long
byte : It is 1 byte(8-bits) integer data type. Value range from -128 to 127. Default value zero.
example: byte b=10;
// Program on Dealing Data - Byte Values.
class Prog9 {
public static void main(String[] args)
{
Byte a;
a = 127; // Range for Byte Data Type is +127 to -128.
System.out.println("a Vlaue is " + a);
a = -128;
System.out.println("a Vlaue is " + a);
}}

}short : It is 2 bytes(16-bits) integer data type. Value range from -32768 to 32767. Default
value zero. example: short s=11;
// Program on Dealing Data - Short Values.
class Prog10 {
public static void main(String[] args)
{
short a;
a = - 32768; // Values Range is -32768 to 32767
System.out.println("a Vlaue is " + a);
a =32767;
System.out.println("a Vlaue is " + a);
}
}

Core JAVA Page 2


int : It is 4 bytes(32-bits) integer data type. Value range from -2147483648 to 2147483647.
Default value zero. example: int i=10;
/ Program on Dealing Data - Inter Values.
class Prog2
{
public static void main(String[] args)
{
int a;
a= 100;
System.out.println(a);
System.out.println("A Vlaue is " + a);
}
}

long : It is 8 bytes(64-bits) integer data type. Value range from -9,223,372,036,854,775,808


to 9,223,372,036,854,775,807. Default value zero. example: long l=100012;
// Program on Dealing Data - Long Values.
class Prog11 {
public static void main(String[] args)
{
long a;
a = 2335L;
System.out.println("a Vlaue is " + a);
a =1234566789l;
System.out.println("a Vlaue is " + a);
}
}

Floating-Point Number
This group includes float, double
float : It is 4 bytes(32-bits) float data type. Default value 0.0f. example: float ff=10.3f;
// Program on Dealing Data - Float Values.
class Prog8 {
public static void main(String[] args)
{
float a;
a = 2335.67f;
System.out.println("a Vlaue is " + a);
a =1234566789.454f;
System.out.println("a Vlaue is " + a);
}
}
double : It is 8 bytes(64-bits) float data type. Default value 0.0d. example: double
db=11.123;
Core JAVA Page 3
/ Program on Dealing Data - Double Values.
class Prog12 {
public static void main(String[] args)
{
double a;
a = 2335.67d;
System.out.println("a Vlaue is " + a);
a =1234566789.45545345534534d;
System.out.println("a Vlaue is " + a);
}
}

Characters
This group represent char, which represent symbols in a character set, like letters and
numbers.
char : It is 2 bytes(16-bits) unsigned unicode character. Range 0 to 65,535. example: char
c='a';
// Program on Dealing Data - Char Values.
class Prog13 {
public static void main(String[] args)
{
char a;
a = 'A';
System.out.println("a Vlaue is " + a);
a ='a';
System.out.println("a Vlaue is " + a);
}
}

Boolean
This group represent boolean, which is a special type for representing true/false values.
They are defined constant of the language.
example: boolean b=true;
class Prog15 {
public static void main(String[] args)
{
boolean res=false;
System.out.println("String strored in s is " + res);
res = true;
System.out.println("String strored in s is " + res);
}
}
Core JAVA Page 4
Non-Primitive(Reference) Data type
A reference data type is used to refer to an object. A reference variable is declare to be of
specific and that type can never be change. We will talk a lot more about reference data
type later in Classes and Object lesson.
Java tokens:

 Reserve
d

Keywords – Reserved keywords are java tokens with predefined meaning. Java has
60 reserved keywords. e.g int, class, break, continue etc.
 Identifiers – Identifiers are java tokens designed and decided by the java
programmer. Examples for java tokens namely identifiers are: name for the class,
name for members of the class, and temporary variables in class methods.
 Literals – Literals are java tokens containing set of characters. Literals are used to
represent a constant that has to be stored in a variable. e.g 2,5,4.6,”ram”,’s’ etc.
 Operators – Operators are java tokens containing a special symbol and predefined
meaning in Java. Operators can be used with one or more operands to achieve a
result. e.g unary, binary, ternary operators
 Variables – Variables are used to hold the value. e.g int a,b;

Operators in JAVA
Operator in java is a symbol that is used to perform operations

Java provides a rich set of operators enviroment. Java operators can be devided into following categories

 Arithmetic operators

 Relation operators

 Logical operators

 Bitwise operators

Core JAVA Page 5


 Assignment operators

 Conditional operators

 Misc operators

1. Arithmetic operators
Arithmetic operators are used in mathematical expression in the same way that are used in algebra.

operator description

+ adds two operands

- subtract second operands from first

* multiply two operand

/ divide numerator by denumerator

% remainder of division

++ Increment operator increases integer value by one

-- Decrement operator decreases integer value by one

Example Program:
class OperatorExample
{
public static void main(String args[])
{
int a=10;
int b=5;
System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0
}
}

Relation operators
The following table shows all relation operators supported by Java.

operator description

== Check if two operand are equal

!= Check if two operand are not equal.

> Check if operand on the left is greater than operand on the right

< Check operand on the left is smaller than right operand

Core JAVA Page 6


>= check left operand is greater than or equal to right operand

<= Check if operand on left is smaller than or equal to right operand

Bitwise operators
Java defines several bitwise operators that can be applied to the integer types long, int, short, char and byte

operator description

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

<< left shift

>> right shift

Logical operators
Java supports following 3 logical operator. Suppose a=1 and b=0;

operator description example

&& Logical AND (a && b) is false

|| Logical OR (a || b) is true

! Logical NOT (!a) is false

The logical && operator doesn't check second condition if first condition is false. It checks
second condition only if first one is true.

The bitwise & operator always checks both conditions whether first condition is true or
false.

Logical && and Bitwise & Example Program


class Ope
ratorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a<b&&a<c);//false && true = false
System.out.println(a<b&a<c);//false & true = false
}}

Core JAVA Page 7


Java OR Operator Example: Logical || and Bitwise |
The logical || operator doesn't check second condition if first condition is true. It checks
second condition only if first one is false.

The bitwise | operator always checks both conditions whether first condition is true or
false.

class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a>b||a<c);//true || true = true
System.out.println(a>b|a<c);//true | true = true
//|| vs |
System.out.println(a>b||a++<c);//true || true = true
System.out.println(a);//10 because second condition is not checked
System.out.println(a>b|a++<c);//true | true = true
System.out.println(a);//11 because second condition is checked
}}
Assignment Operators
Assignment operator supported by Java are as follows

operator description example

= assigns values from right side operands to left side operand a=b

+= adds right operand to the left operand and assign the result to left a+=b is same as a=a+b

-= subtracts right operand from the left operand and assign the result to left operand a-=b is same as a=a-b

*= mutiply left operand with the right operand and assign the result to left operand a*=b is same as a=a*b

/= divides left operand with the right operand and assign the result to left operand a/=b is same as a=a/b

%= calculate modulus using two operands and assign the result to left operand a%=b is same as a=a%b

Java Assignment Operator Example


class OperatorExample{
public static void main(String[] args){
int a=10;
a+=3;//10+3
System.out.println(a);
a-=4;//13-4
System.out.println(a);
a*=2;//9*2

Core JAVA Page 8


System.out.println(a);
a/=2;//18/2
System.out.println(a);
}}

Control statements:

These statements are used to control the compiler execution at run time and
execute the statements according to the given conditions.

These are of following 4 types

1) Conditional statements
2) Select statement
3) Jumping statement
4) Iterative statements (or loops)
Conditional statements:

These statements are executing the statements depending up on given condition.

 There are following 4 types of conditional statements.

1) Simple if

2) if .. else

3) Nested if

4) else..if ladder

1) Simple if:

Syntax:

if (condition)

Statements;

In the above structure the statements are


executed when the specified condition is true otherwise
compiler avoids the statements from execution.

Core JAVA Page 9


2) if..else:
Syntax:
if(condition)
{
statemetns1;
}
else
{
statemetns2;
}
In the above structure if specified condition is true then
statements1 are executed otherwise statements2 are executed.

Core JAVA Page 10

You might also like