Professional Documents
Culture Documents
But,
IDENTIFIERS:
Identifiers are the names given to variables, methods, objects, class,
and constants.
KEYWORDS:
Keywords have specific meaning to the compiler and cannot be used
for other Purposes in the program.
DATA TYPES:
The Java programming language is strongly-typed, which means that
all variables must first be declared before they can be used.
From these eight primitives data types 4 are Integer types, 2 are
floating point types, 1 is Character type, and 1 is Boolean type.
Mr. Prashant Kulkarni 3
byte:
The byte data type is an 8-bit signed two's complement integer.
Range : -128 to 127.
short:
The short data type is a 16-bit signed two's complement integer.
Range : -32,768 to 32,767.
int:
The int data type is a 32-bit signed two's complement integer.
Range : -2,147,483,648 to 2,147,483,647.
long:
The long data type is a 64-bit signed two's complement integer.
Range : -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float:
The float data type is a single-precision 32-bit IEEE 754 floating
point.
boolean:
The boolean data type has only two possible values: true and false.
Use this data type for simple flags that track true/false conditions.
This data type represents one bit of information.
char:
The char data type is a single 16-bit Unicode character.
Range : '\u0000' (or 0) to '\uffff' (or 65,535).
Java use Unicode character scheme for char type.
The method must be both public and static for the program to run
correctly.
static is a keyword that states that all instances of a given class are to
share the same variable/method.
String is a special class built into the Java language defined in the
java.lang package.
Note:
Source file always has the same name as that of class.
e.g.
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println ("Hello, world!");
}
}
Note:
Name of above source file is Helloworld.java
Mr. Prashant Kulkarni 11
2. Compile the source file into .class file:
The Java compiler (javac) takes your source file
and translates into instructions that the JVM can understand.
if statement:
This statement is used to decide whether certain statements will be
executed or not.
Syntax:
if(condition)
{
// Statements to execute if
// condition is true
}
If-else statement:
This statement is used to execute certain block of statements
if condition is True and execute another block of statement
if condition is False.
Syntax:
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
Mr. Prashant Kulkarni 17
}
Decision Making Statements:
Nested if statement:
It means an if statement inside an if statement.
Syntax:
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
class Test
{
public static void main(String[] args)
{
int x = 10;
if (x)
{
System.out.println("HELLO");
}
else
{
System.out.println("BYE");
}
}
}
Mr. Prashant Kulkarni 22
Sample Code:
class Test
{
public static void main(String[] args)
{
int x = 10;
if (x) // Compile time error
{
System.out.println("HELLO");
}
else
{
System.out.println("BYE");
}
}
}
Mr. Prashant Kulkarni 23
Output:
public
class Test {
public
static void main(String[] args)
{
int x = 10, y = 20;
if (x < y) {
if (x > y) {
System.out.println(“Good Morning");
} else {
System.out.println(“Good Afternoon");
}
}
}
}
Mr. Prashant Kulkarni 24
Output:
public
class Test {
public
static void main(String[] args)
{
int x = 10, y = 20;
if (x < y) {
if (x > y) {
System.out.println(“Good Morning");
} else {
System.out.println(“Good Afternoon");
}
}
}
}
Mr. Prashant Kulkarni 25
Output:
DayName="saturday";
switch(DayName)
{
case "sunday":
System.out.println("wooo... hoooo.... It's Sunday");
break;
case "monday":
System.out.println("oh no! Its Monday");
break;
case "wednesday":
System.out.println("Wednesday is so boring..");
break;
case "saturday":
System.out.println("It's Saturday.. Party Night!!!");
break;
default:
System.out.println("Invalid Day Name");
} Mr. Prashant Kulkarni 26
Output:
DayName="saturday";
switch(DayName)
{
case "sunday":
System.out.println("wooo... hoooo.... It's Sunday");
break;
case "monday":
System.out.println("oh no! Its Monday");
break;
case "wednesday":
System.out.println("Wednesday is so boring..");
break;
case "saturday":
System.out.println("It's Saturday.. Party Night!!!");
break;
default:
System.out.println("Invalid Day Name");
} Mr. Prashant Kulkarni 27
Control Structure:
class Test
{
public static void main(String[] args)
{
int n = 5;
for (int i = 1; i <= n; ++i)
{
System.out.println("Java is fun");
}
}
}
class Test
{
public static void main(String[] args)
{
int n = 5;
for (int i = 1; i <= n; ++i)
{
System.out.println("Java is fun");
}
}
}
Java is fun
Java is fun
Java is fun
Java is fun
Java is fun
Mr. Prashant Kulkarni 29
Control Structure:
class Test
{
public static void main(String[] args)
{
int n = 5;
for (int i = 1; i <= n; ++i)
{
System.out.println(i);
}
}
}
1
2
3
4
5