Java - Unit I
Java - Unit I
Unit - I
JAVA
programming language and a platform independent.
Java is a high level, robust, object-oriented and secure
programming language.
developed by Sun Microsystems – 1995
father of Java -James Gosling
Before java – named as OAK
Rename to JAVA
History
Originally designed for interactive television
but it was too advanced technology for the
digital cable television industry at the time.
to develop a language for digital devices such as
set-top boxes, televisions, etc.
best suited for internet programming.
Later, Java technology was incorporated by
Netscape.
Principle / features of java :
◦ Simple, Robust, Portable, Platform-independent, Secured, High
7) Why had they chose the name Java for Java language?
◦ According to James Gosling, "Java was one of the top choices along
with Silk". Since Java was so unique, most of the team members
chosen by James Gosling while having a cup of coffee nearby his office.
OOPs concepts
allocated in memory.
In other words, it is a name of the memory
location.
It is a combination of "vary + able" which
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
if (condition) {
// block of code will execute if the condition i
s true
}
For example
var x = 78;
if (x>70) {
console.log("x is greater")
}
The if….else statement
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;
}
}
Looping
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
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been
executed.
The example below will print the numbers 0 to 4:
}
While Loop
Syntax
while (condition)
{ // code block to be executed
}
Example
int i = 0;
while (i < 5)
{
System.out.println(i); i++;
}
do...while
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);
Output
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
Labeled Loops
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
One-dimensional Array
Also known as a linear array, the elements
objects.
The most direct way to create a string is to write −
class Test {
public static void main(String[] args)
{
// Declare String without using new operator
String s = "GeeksforGeeks";