You are on page 1of 36

Programming Concepts of JAVA

Mr. Prashant Kulkarni 1


Java syntax is based on the syntax of C and C++.

But,

Many of the difficult parts of C and C++, like pointers, operator


overloading, and multiple inheritance have been eliminated.

Java provides class libraries for strings, graphics, concurrency, and


exception handling.

Java also provides graphical user interface classes.

Mr. Prashant Kulkarni 2


Some of the programming concepts is as follows.

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.

Java programming language supports eight primitive data types.

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.

Mr. Prashant Kulkarni 4


double:
The double data type is a double-precision 64-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.

Mr. Prashant Kulkarni 5


Data types and its default values:

Data Type Default Value (for fields)


byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
String (or any object) null
boolean false

Mr. Prashant Kulkarni 6


Comments:
Single-Line Comments:
Short comments can appear on a single line indented to the level of
the code that follows.
e.g.
if (condition)
{
...
z = x + y; // Handle the condition.
...
}
Multi-Line Comments:
It can appear on more than one line.
e.g.
{
...
/* . . . . .
. . . . .*/
} Mr. Prashant Kulkarni 7
First Java Program:
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println ("Hello, world!");
}
}
Where,
public: public is a Java keyword which declares a member's access
as public.
Public members are visible to all other classes. This means
that any other class can access a public field or method.

class: class is a Java keyword which begins the declaration and


definition of a class.

Mr. Prashant Kulkarni 8


public static void main (String[] args):

It is Java method named main.


The main method is where Java will start executing the program.

args is a method parameter which will contain the command line


arguments used to run the program.

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.

Mr. Prashant Kulkarni 9


void is a Java keyword specify that the method does not return any
type, the method returns void.

String is a special class built into the Java language defined in the
java.lang package.

System.out.println ("Hello, world!") :


This statement sends the text Hello, world! to the console.

Mr. Prashant Kulkarni 10


Creating First Application:
1. Create a source file:
A source file contains code, written in the Java programming language.

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.

The file containing these instructions is known as bytecode.

Open up a shell, or command window as follows.

Mr. Prashant Kulkarni 12


To compile your source file,
change your current directory where your file is located.

At the prompt, type the following command and press Enter.


C: \ java> javac HelloWorld.java

The compiler has generated a bytecode file, HelloWorld.class.


This can be used to run our program.

Mr. Prashant Kulkarni 13


3. Run the program:
The Java application launcher tool (java) uses the Java virtual
machine to run your application.

C: \ java> java HelloWorld

Mr. Prashant Kulkarni 14


Control Structures and Decision Making Statements:

1. The if-then Statements


1. The if-then-else Statement
2. The if-then-elseif Statement:
2. The switch-case Statement
3. The while Statement
4. The do - while Statement
5. The for Statement
6. Branching Statements
1. The break Statement
2. The continue Statement

Mr. Prashant Kulkarni 15


Decision Making Statements:

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
}

Mr. Prashant Kulkarni 16


Decision Making Statements:

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
}
}

Mr. Prashant Kulkarni 18


Decision Making Statements:
If-else-if Ladder statement:
Syntax:
if (condition)
statement;
else if (condition)
statement;
..
..
else
statement;

Mr. Prashant Kulkarni 19


Decision Making Statements:
Switch-Case statement:
Syntax:
switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
..
case valueN:
statementN;
break;
default:
statementDefault;
Mr. Prashant Kulkarni 20
}
Control Structure:
It is used to execute set of instructions repeatedly while some condition
evaluates to True.

Initialization statement ; Intialization Statement;


while(condition) do
{ {
Statement; Statement;
Statement; Statement;
Inc / Dec statement; Inc / Dec statement;
} }while(condition);

for(Initialization; Condition; Inc/Dec)


{
Statement;
Statement;
}
Mr. Prashant Kulkarni 21
Sample Code:

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");
}
}
}

Mr. Prashant Kulkarni 28


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");
}
}
}

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);
}
}
}

Mr. Prashant Kulkarni 30


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

Mr. Prashant Kulkarni 31


Control Structure:
int i = 0;
while (i < 5)
{
System.out.println("Learning Java is Fun");
System.out.println("The value of i is = " + i);
i++;
}

Mr. Prashant Kulkarni 32


Control Structure:
int i = 0;
while (i < 5)
{
System.out.println("Learning Java is Fun");
System.out.println("The value of i is = " + i);
i++;
}

Learning Java is Fun


The value of i is = 0
Learning Java is Fun
The value of i is = 1
Learning Java is Fun
The value of i is = 2
Learning Java is Fun
The value of i is = 3
Learning Java is Fun
The value of i is = 4
Mr. Prashant Kulkarni 33
Control Structure:
int i = 0;
do {
i++;
System.out.println("Learning Java is Fun");
System.out.println("The value of i is " + i);
} while ( i != 5 );

Mr. Prashant Kulkarni 34


Control Structure:
int i = 0;
do {
i++;
System.out.println("Learning Java is Fun");
System.out.println("The value of i is " + i);
} while ( i != 5 );

Learning Java is Fun


The value of i is = 0
Learning Java is Fun
The value of i is = 1
Learning Java is Fun
The value of i is = 2
Learning Java is Fun
The value of i is = 3
Learning Java is Fun
The value of i is = 4
Learning Java is Fun
Mr. Prashant Kulkarni 35
The value of i is = 5
Control Structure Comparison:

Comparison For loop While Loop do-while loop

It iterates a block of code This iterates a block of It checks for the


Introduction for a known number of code based on the condition after the
times output of a boolean statement is executed for
condition that particular iteration.

It should be used when When the number of If there is a necessity for


the number of iterations iterations is not fixed, executing the loop at
When to use is constant and known while loop can be used least once whilst having
before the execution of to iterate. no knowledge about the
the program. number of iterations, the
do while loop has to be
used.
for(initializing statement; while(condition) do
testing condition; { {
Syntax inc/dec) //statements; //statements
{ } }
//code to be iterated While(condition);
}
Mr. Prashant Kulkarni 36

You might also like