Ss Java Basics
Ss Java Basics
PROGRAMMING
Birth of Java
•Assembly level language created by the assembler can be executed only the same
computer which has same OS and CPU in which program was developed and this
makes all S/W developed using this language dependent on the platform.
•In 1990 a team of Researchers led by James Gosling starts research to develop a
platform independent programming language.
•In the year 1995 official version of JAVA was first released to the market
Birth of Java
JDK(Java Development Kit)
-------
----
-----
Javac Bytecode JVM
Java compiler Java Virtual
----- Machine
---
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/
_keywords.html
Identifiers
•Identifiers are the names given for an entity in a program
•In java identifiers are used to identify Class, Methods and Variables.
RULES OF IDENTIFIERS.
•A named memory location which is used to store values for the program is
called as variable.
✓ Declaration
✓ Initialization
✓ Utilization
•Declaration is a statement which written to specify
the type of data to be stored in the given variable.
syntax : datatype varName; D
DataType Capacity Default value
byte 8-bits or 1-byte 0
short 16-bits or 2-bytes 0
int 32-bits or 4-bytes 0
long 64-bits or 8-bytes 0l
float 32-bits or 4-bytes 0.0f
double 64-bits or 8-bytes 0.0
char 16-bits or 2-bytes Space
Boolean 8-bits or 1-byte false
Initialization
• Initialization : It is a statement which is written to store the data
within a variable using assignment operator(=).
syntax : varName= value;
Utilization
• Utilization : It is one or group of statements which is written to use
the value in the variable to perform the operation.
Initialization
• Initialization : It is a statement which is written to store the data
within a variable using assignment operator(=).
syntax : varName= value;
Utilization
• Utilization : It is one or group of statements which is written to use
the value in the variable to perform the operation.
DATA TYPES
DATA TYPES
double num1=10250250.115511254515;
System.out.println("num1: "+num1);
}
}
boolean: boolean data type represents one bit of information.
There are only two possible values : true and false.
This data type is used for simple flags that track true/false conditions.
Default value is false.
Example : boolean one = true
Types of operators
✓ Logical Operators
&& || ^
✓ Increment and Decrement Operators
++ --
✓ Arithmetic Operators
+ -* / %
✓ Comparison Operators or Relational Operators
< > == != <= >=
✓ Logical Operators
&& || ^
✓ Bitwise Operators
&|^
Priority Operator Operation
1 () [] -> . :: Function call, scope, array/member access
2 ! ~ -+ * & (most) unary operators, sizeof and type casts(right to left)
sizeof type cast++ --
3 * / % MOD Multiplication, division, modulo
4 +- Addition and subtraction
5 << >> Bitwise shift left and right
6 < <= > >= Comparisons: less-than and greater-than
7 == != Comparisons: equal and not equal
8 & Bitwise AND
9 ^ Bitwise exclusive OR (XOR)
10 | Bitwise inclusive (normal) OR
11 && Logical AND
12 || Logical OR
13 ?: Conditional expression (ternary)
14 =, +=, -=, *=, /=, %=, Assignment operators (right to left)
&=, |=, ^=, <<=, >>=
15 , Comma operator
Arithmetic Operators
In the example below, we use the assignment operator (=) to assign the
value 10 to a variable called x:
Example
int x = 10;
Example
int x = 10; x += 5;
public class Main {
public static void main(String[] args) {
int x = 5;
A list of all assignment operators: System.out.println(x);
x += 3;
System.out.println(x);
Operator Example Same As x -= 3;
= x=5 x=5 System.out.println(x);
+= x += 3 x=x+3 x *= 3;
System.out.println(x);
-= x -= 3 x=x-3
x /= 3;
*= x *= 3 x=x*3
System.out.println(x);
/= x /= 3 x=x/3 x %= 3;
%= x %= 3 x=x%3 System.out.println(x);
}
}
Comparison Operators
Comparison operators are used to compare two values (or variables). This is important in
programming, because it helps us to find answers and make decisions.
These values are known as Boolean values, and you will learn more about them in
the Booleans and If..Else chapter.
In the following example, we use the greater than operator (>) to find out if 5 is greater than
3:
Example
int x = 5; int y = 3; System.out.println(x > y); // returns true, because
5 is higher than 3
A list of all Comparison operators:
System.out.println(x <= y); // returns false because 5 is neither less than or equal
to 3
}
}
Logical Operators
You can also test for true or false values with logical operators.
Logical operators are used to determine the logic between variables or values:
Logical OR ||operator will return FALSE if and only if the result of both the
Boolean condition’s result is FALSE and in all other cases it returns TRUE
Logical XOR ^operator will return FALSE if and only if the result of both the
Boolean condition’s result is FALSE or TRUE and in all other cases it returns TRUE
Bitwise operator will perform bitwise operations on every bit of given value and
produce results as 0 or 1
25
2|12-1
2|6-0 0011001 0011001 0011001
2|3-0 1100100(&) 1100100(|) 1100100(^)
2|1-1
Result : 11001 -------------- -------------- --------------
100 0000000 1111101 1111101
2|50-0 0*2^6+ 0*2^5+
2|25-0 0*2^4+ 0*2^3+
2|12-1 0*2^2+ 0*2^1+
2|6-0 0*2^0 =
2|3-0 RESULT : 0
2|1-1
Result : 1100100
Concatenation Operator
• Methods are named blocks of codes which will perform a specific task.
•Return type : depends on the data type of the value returned from the method
• A method which is called by another method is known as called method.
• A method which is calling another method is known as calling method.
• If methods returns any value then the returned value should be
stored in a variable matching the return type of the method.
• return statement is used to transfer the control and the values from
called method to calling method.
• If methods do not return any value then its return type should be
declared as void.
• If return type of method is void then, programmer can skip writing the
return statement.
FLOW CONTROL STATEMENTS
Flow control statements
•IF
•If-else
•if-else-if
•Switch-case
if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) { Since the value of number is 10, it
System.out.println("The number is negative."); satisfies the first condition, and
} else { the output will be:
System.out.println("The number is zero.");
The number is positive.
}
}
}
if –else -if statement
•It is a type of decision making statements which is used to whenever there are
multiple Boolean conditions to be evaluated to execute different set of
statements.
Syntax : if(Boolean cond1)
{
stmt..
}
else if(Boolean cond2)
{
stmt
}
else
{
stmt
}
switch case -statement
Statement 1 is executed (one time) before the execution of the code block.
Statement 3 is executed (every time) after the code block has been executed.
The "inner loop" will be executed one time for each iteration of the "outer loop":
Example
The do/while loop is a variant of the while loop. This loop will
execute the code block once, before checking if the condition is
true, then it will repeat the loop as long as the condition is true.
Syntax
do
{
// code block to be executed
} while (condition);
for-each loop
The "for-each" loop is also known as the enhanced for loop or the "for-
each" loop. It allows you to iterate over elements in an array or any
object that implements the Iterable interface (e.g., List, Set, etc.).
The loop simplifies the process of iterating through a collection and
accessing its elements without dealing with indices explicitly.
Output
public class ForEachExample {
public static void main(String[] args) { 1
int[] numbers = {1, 2, 3, 4, 5}; 2
3
for (int number : numbers) {
System.out.println(number); 4
} 5
}
}