UNIT-I JAVA FUNDAMENTALS
UNIT-I JAVA FUNDAMENTALS
Simple.
Object-oriented.
Distributed.
Interpreted.
Robust.
Secure.
Architecture neutral.
Portable.
1. Primitive data types: The primitive data types include boolean, char,
byte, short, int, long, float and double.
2. Non-primitive data types: The non-primitive data types
include Classes, Interfaces, and Arrays.
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
Example:
1. double d1 = 12.3
Example:
Java Variables
A variable is a container which holds the value while the Java program
Types of Variables
There are three types of variables in Java
o local variable
o instance variable
o static variable
1) Local Variable
A variable declared inside the body of the method is called local variable.
You can use this variable only within that method and the other methods
in the class aren't even aware that the variable exists.
A local variable cannot be defined with "static" keyword.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is
called an instance variable. It is not declared as static
3) Static variable
1. public class A
2. {
3. static int m=100;//static variable
4. void method()
5. {
6. int n=90;//local variable
7. }
8. public static void main(String args[])
9. {
10. int data=50;//instance variable
11. }
12.}//end of class
Java Arrays
Normally, an array is a collection of similar type of elements which has
contiguous memory location.
Array in Java is index-based, the first element of the array is stored at the
0th index, 2nd element is stored on 1st index and so on.
Output:
10
20
70
40
50
. The Java for-each loop prints the array elements one by one. It holds an array element in a variable,
then executes the body of the loop.
Output:
123
245
445
Operators in Java
Operator in Java
There are many types of operators in Java which are given below:
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.
Output:
15
5
50
2
0
Output:
1. 40
2. 80
3. 80
4. 240
false
false
2
Java Control Statements | Control Flow in Java
2. Loop statements
o do while loop
o while loop
o for loop
o for-each loop
3. Jump statements
o break statement
o continue statement
There are two types of decision-making statements in Java, i.e., If statement and switch statement.
1. Simple if statement
2. if-else statement
3. if-else-if ladder
4. Nested if-statement
Simple if statement
if(condition) {
statement 1; //executes when condition is true
}
Ex:
Student.java
x + y is greater than 20
2) if-else statement
is an extension to the if-statement, which uses another block of code, i.e., else block. The else block
is executed if the condition of the if-block is evaluated as false.
Syntax:
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
Student.java
Output:
x + y is greater than 20
Nested if-statement
In nested if-statements, the if statement can contain a if or if-else statement inside another if or
else-if statement.
if(condition 1) {
statement 1; //executes when condition 1 is true
if(condition 2) {
statement 2; //executes when condition 2 is true
}
else{
statement 2; //executes when condition 2 is false
}
}
Ex:
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main(String args[])
{
int a=10;
int b=20;
if(a==10){
if(b==20){
System.out.println("GeeksforGeeks");
}
}
}
}
O/P:
GeeksforGeeks
Code explanation :
In the first step, we have imported the required packages.
In the next step, we have created a class names GFG
In the next step, we have written the main method.
Within the main method, we have assigned values to variables.
Using nested if conditions, we have printed a statement.
Here two statements are true. Hence statement was executed successfully.
Switch Statement in Java
The switch statement is a multi-way branch statement. In simple words, the Java switch
statement executes one statement from multiple conditions.
1. There can be any number of cases just imposing condition check but remember duplicate
case/s values are not allowed.
2. The value for a case must be of the same data type as the variable in the switch.
3. The value for a case must be constant or literal. Variables are not allowed.
4. The break statement is used inside the switch to terminate a statement sequence.
5. The break statement is optional. If omitted, execution will continue on into the next case.
6. The default statement is optional and can appear anywhere inside the switch block. In case, if
it is not at the end, then a break statement must be kept after the default statement to omit
the execution of the next case statement.
Syntax: Switch-case
// switch statement
switch(expression)
{
// case statements
// values must be of same type of expression
case value1 :
// Statements
break; // break is optional
case value2 :
// Statements
break; // break is optional
// Case
case 1:
dayString = "Monday";
break;
// Case
case 2:
dayString = "Tuesday";
break;
// Case
case 3:
dayString = "Wednesday";
break;
// Case
case 4:
dayString = "Thursday";
break;
// Case
case 5:
dayString = "Friday";
break;
// Case
case 6:
dayString = "Saturday";
break;
// Case
case 7:
dayString = "Sunday";
break;
// Default case
default:
dayString = "Invalid day";
}
System.out.println(dayString);
}
}
Ex:
// Java program to illustrate using
// break to exit a loop
class BreakLoopDemo {
public static void main(String args[])
{
// Initially loop is set to run from 0-9
for (int i = 0; i < 10; i++) {
// terminate loop when i is 5.
if (i == 5)
break;
Output:
i: 0
i: 1
i: 2
i: 3
i: 4
Loop complete.
Continue statement is often used inside in programming languages inside loops control structures.
Inside the loop, when a continue statement is encountered the control directly jumps to the
beginning of the loop for the next iteration instead of executing the statements of the current
iteration.
Syntax: continue keyword along with a semicolon
continue;
Ex:
//Java Program to demonstrate the use of continue statement
//inside the for loop.
public class ContinueExample {
public static void main(String[] args) {
//for loop
for(int i=1;i<=10;i++){
if(i==5){
//using continue statement
continue;//it will skip the rest statement
}
System.out.println(i);
}
}
Output:
1
2
3
4
6
7
8
9
10
Ex:
/*package whatever //do not write package name here */
// Java program to write a code in for loop from 1 to 10
class GFG {
public static void main(String[] args)
{
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}
Output
1
2
3
4
5
6
7
8
9
10
Java while loop with Examples
Syntax:
while (test_expression)
{
// statements
}
Ex:
// Java program to illustrate while loop.
class whileLoopDemo {
public static void main(String args[])
{
// initialization expression
int i = 1;
// test expression
while (i < 6) {
System.out.println("Hello World");
// update expression
i++;
}
}
}
Output
Hello World
Hello World
Hello World
Hello World
Hello World
Java do-while loop with Examples
Loops in Java come into use when we need to repeatedly execute a block of statements. Java do-
while loop is an Exit control loop.
Syntax:
do
{
// Loop Body
Update_expression
}
// Condition check
while (test_expression);
Ex:
// Java Program to Illusterate One Time Iteration
// Inside do-while Loop
// When Condition IS Not Satisfied
// Class
class GFG {
do {
// Checking condition
// Note: It is being checked after
// minimum 1 iteration
while (i < 0);
}
}
Output
Print statement
For-each loop in Java
for-each Loop Sytnax
The syntax of the Java for-each loop is:
Here,
class Main {
public static void main(String[] args) {
// create an array
int[] numbers = {3, 9, 5, -5};
3
9
5
-5
TWO MARKS:
Instance Variable: These variables are declared within a class but outside a
method, constructor, or block and always get a default value.
These variables are usually created when we create an object and are
destroyed when the object is destroyed.
We may use an access specifier, for instance, variable, and if no access
specifier is specified, then the default access specifier is used.
Each and every object will have its own copy of instance variables.
Local Variable: These variables are declared within a method but do not get
any default value.
They are usually created when we enter a method or constructor and are
destroyed after exiting the block or when the call returns from the method.
Its scope is generally limited to a method and its scope starts from the line
they are declared. Their scope usually remains there until the closing
curly brace of the method comes.
The initialization of the local variable is mandatory.
Example