Java - Unit I
Java - Unit I
Java - Unit I
◦ The team gathered to choose a new name. The suggested words were
◦ According to James Gosling, "Java was one of the top choices along
with Silk". Since Java was so unique, most of the team members preferred
8) Java is an island in Indonesia where the first coffee was produced (called
Java coffee). It is a kind of espresso bean. Java name was chosen by James
local variable
instance variable
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
It is called an instance variable because its value is
instance-specific and is not shared among instances.
3) Static variable
A variable that is declared as static is called a static
variable. It cannot be local. You can create a single
copy of the static variable and share it among all the
instances of the class.
Memory allocation for static variables happens only
once when the class is loaded in the memory.
public class A
{
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
public static void main(String args[])
{
int data=50;//instance variable
}
}//end of class
public class Simple
{
public static void main(String args[])
{
int a=10;
int b=10;
int c=a+b;
System.out.println(c);
}
}
Type casting is when you assign a value of one
primitive data type to another type.
In Java, there are two types of casting:
Widening Casting (automatically) - converting a
smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double
Narrowing Casting (manually) - converting a larger
type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
Widening Casting
Widening casting is done automatically when
passing a smaller size type to a larger size
type:
Narrowing Casting
Narrowing casting must be done manually by
placing the type in parentheses in front of the
value:
Operators are used to perform operations on
variables and values.
default:
code to be executed if all cases are not matched;
}
var num = 5;
switch(num) {
case 0 : {
console.log("Sunday");
break;
}
case 1 : {
console.log("Monday");
break;
}
case 2 : {
console.log("Tuesday");
break;
}
case 3 : {
console.log("Wednesday");
break;
}
case 4 : {
console.log("Thursday");
break;
}
case 5 : {
console.log("Friday");
break;
}
case 6 : {
console.log("Saturday");
break;
}
default: {
console.log("Invalid choice");
break;
}
}
Loops can execute a block of code as long as a specified
condition is reached.
Loops are handy because they save time, reduce errors, and they
make code more readable.
For Loop
}
The while loop loops through a block of code
as long as a specified condition is true:
Syntax
while (condition)
{ // code block to be executed
}
Example
int i = 0;
while (i < 5)
{
System.out.println(i); i++;
}
A do...while loop is similar to a while loop,
except that a do...while loop is guaranteed to
execute at least one time.
Syntax
Following is the syntax of a do...while loop −
Do
{ // Statements
}
while(Boolean_expression);
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
What is a labeled loop in Java? A label is a valid
variable name that denotes the name of the loop to
where the control of execution should jump.
To label a loop, place the label before the loop with a
colon at the end. Therefore, a loop with the label is
called a labeled loop.
public class LabeledForLoop
{
public static void main(String args[])
{
int i, j;
for(i=1;i<=5;i++)
{
System.out.println();
//inner loop
inner: //label
for(j=1;j<=10;j++)
{
System.out.print(j + " ");
if(j==9)
break inner;
}
}
}
}
Arrays are used to store multiple values in a
single variable, instead of declaring separate
variables for each value.
To declare an array, define the variable type
with square brackets:
// declare an array
int[] age = new int[5];
// initialize array
age[0] = 12;
age[1] = 4;
age[2] = 5;
//declare and initialize an array
int[] age = {12, 4, 5, 2, 5};
One-dimensional Array
Also known as a linear array, the elements are
stored in a single row. For example:
In Java, 2D arrays are stored as arrays of
arrays.
Therefore, the way 2D arrays are declared is
similar 1D array objects.
2D arrays are declared by defining a data type
followed by two sets of square brackets.
The syntax to declare and initialize the 2D
array is given as follows.
int arr[2][2] = {0,1,2,3};
Strings, which are widely used in Java programming,
are a sequence of characters.
In Java programming language, strings are treated as
objects.
The most direct way to create a string is to write −
String greeting = "Hello world!";
import java.io.*;
import java.lang.*;
class Test {
public static void main(String[] args)
{
// Declare String without using new operator
String s = "GeeksforGeeks";