Java Notes1 PDF
Java Notes1 PDF
To compile the example.java program, execute the compiler at the command prompt
– javac example.java
The above statement will create a file called ‘example.class’ which contains the
bytecode.
To execute the program use Java interpreter at the command prompt – java example
Prepared By – Asmita Ranade
Understanding Java Program
/* First Java Program - example.java */ Comment
class example class keyword defines
{ a new class
public static void main(String[] args) Program execution
{ begins at main( )
System.out.println(“Java Programming”);
}
}
public keyword is the access modifier. Here, the main( ) is public, so it can be
accessed from outside. static keyword allows main( ) to be called by JVM before any
object of the class is created. Prepared By – Asmita Ranade
Understanding Java Program
/* First Java Program - example.java */ void keyword tells
the compiler that
class example main( ) does not
{ return a value.
public static void main(String[] args) args is the parameter
in the main( ) which
{ receives command
System.out.println(“Java Programming”); line arguments
} Many statements in
} java end with
semicolon
println( ) displays the string passed to it.
System is a predefined class which provides access to the system.
out is the output stream which represents the console. Prepared By – Asmita Ranade
Variables in Java
A variable is a named memory location in which a value can be stored.
Value of a variable can be changed during the execution of the program.
All Java variables must be identified with unique names or identifiers.
Rules for naming variables –
• Name can contain alphabets, digits, underscores and dollar sign.
• Name can begin with a lowercase alphabet, $ or _.
• Names cannot contain whitespace.
• Reserved keywords cannot be used as variable names.
• Names are case sensitive.
Prepared By – Asmita Ranade
Types of Variables in Java
There are 3 types of variables in Java –
• Local Variable – Local variable is a variable which is declared in local scope.
E.g. a variable declared inside a method.
‘Static’ keyword cannot be used for local variables.
• Instance Variable – These are non-static variables which are declared inside a
class but outside the method. The values of these variables are instance specific.
• Static Variable – Static variable is declared with ‘static’ keyword. It cannot be a
local variable. Single copy of a static variable exists and is shared among all the
instances of the class. Memory is allocated only once to the static variable when
the class is loaded. Prepared By – Asmita Ranade
Scope of Variables in Java
Scope refers to the region in the entire program where the variable is
accessible.
• Class Scope – Local variables which are declared inside a class with private
access modifier but outside any method has a class scope. So, these variables are
accessible everywhere in the class but not outside the class.
‘Static’ keyword cannot be used for local variables.
• Method Scope – Variables which are declared inside a method have method
scope. They are not accessible outside the method.
• Loop Scope – Variables which are declared inside a loop have loop scope. They
are not accessible outside the loop. Prepared By – Asmita Ranade
Primitive Data types in Java
Data type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32768 to 32767
int 4 bytes Stores whole numbers from -2147483648 to 2147483647
Stores whole numbers from -9223372036854775808 to
long 8 bytes
9223372036854775807
Stores fractional numbers. Sufficient for storing 6 to 7
float 4 bytes
decimal digits.
Stores fractional numbers. Sufficient for storing 15 decimal
double 8 bytes
digits.
boolean 1 bit Stores true or false values
char 2 bytes Stores a single character/letter or ASCII values
Prepared By – Asmita Ranade
Java Operators
For performing operations on variables and values, operators are used.
Java provides with following operators –
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise operators
Java’s type wrappers are classes that encapsulate or wrap the primitive data types.
The type wrappers are –
• Ternary operator
• if statement
• if – else statement
• if – else – if ladder
• nested if statement
• switch case structure