Unit-1 Introduction To Java
Unit-1 Introduction To Java
4. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit.
6. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK.
3 Approach It uses the top-down approach. It uses the bottom-up approach. It also uses the bottom-up approach.
4 Dynamic or Static It is a static programming language. It is also a static programming language. It is a dynamic programming language.
5 Code Execution The code is executed directly. The code is executed directly. The code is executed by the JVM.
6 Platform Dependency It is platform dependent. It is platform dependent. It is platform-independent because of byte
code.
7 Translator It uses a compiler only to translate the It also uses a compiler only to translate the Java uses both compiler and interpreter and
code into machine language. code into machine language. it is also known as an interpreted language.
8 File Generation It generates the .exe, and .bak, files. It generates .exe file. It generates .class file.
12 Union and It supports union and structure data It also supports union and structure It does not support union and structure data types.
Structure types. data types.
Datatype
13 Pre-processor It uses pre-processor directives such as It uses pre-processor directives It does not use directives but uses packages.
Directives #include, #define, etc. such as #include, #define,
#header, etc.
14 Constructor/ It does not support constructor and It supports both constructor and It supports constructors only.
Destructor destructor. destructor.
15 Exception It does not support exception handling. It supports exception handling. It also supports exception handling.
Handling
16 Memory It uses the calloc(), malloc(), free(), It uses new and delete operator to It uses a garbage collector to manage the memory.
Management and realloc() methods to manage the manage the memory.
memory.
17 Overloading It does not support the overloading Method and operator overloading Only method overloading can be achieved.
concept. can be achieved.
18 goto Statement It supports the goto statement. It also supports the goto It does not support the goto statements.
statement.
19 Used for It is widely used to develop drivers and It is widely used for system It is used to develop web applications, mobile
operating systems. programming. applications, and windows applications.
defined behavior that we can reason from, without caring about the
• Inheritance : acquiring all the properties from the base class even it is not
Example :
{
public abstract withdraw();
public abstract balanceEnquiry();
public abstract deposit();
public abstract pinChange();
}
10/06/2024 R.SATHEESH , ASST.PROF, CSIT, MLRITM 10
A WAY OF VIEWING WORLD
• Agents
• Responsibilities
• Messages
• Methods
• Initially designed for small, embedded systems in electronic appliances like set-top boxes.
• Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt.
• After that, it was called Oak and was developed as a part of the Green project.
• Why Oak? Oak is a symbol of strength and chosen as a national tree of many countries like the U.S.A., France, Germany,
Romania, etc.
• In 1995, Oak was renamed as "Java" because it was already a trademark by Oak Technologies.
• Java is an island of Indonesia where the first coffee was produced (called java coffee). It is a kind of espresso bean. Java name
was chosen by James Gosling while having coffee near his office.
• Initially developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle Corporation) and released in 1995.
• JDK 1.0 released in(January 23, 1996). After the first release of Java, there have been many additional features added to the
language.
• Now Java is being used in Windows applications, Web applications, Enterprise applications, Mobile applications, cards, etc. Each
new version adds the new features in Java.
10/06/2024 R.SATHEESH , ASST.PROF, CSIT, MLRITM 12
JAVA BUZZWORDS
Java is the most popular object-oriented programming language. Java has many advanced features, a list of
key features is known as Java Buzz Words. The java team has listed the following terms as java buzz words.
• SIMPLE
• SECURE
• PORTABLE
• OBJECT-ORIENTED
• ROBUST
• ARCHITECTURE-NEUTRAL (OR)
PLATFORM INDEPENDENT
• MULTI-THREADED
• INTERPRETED
• HIGH PERFORMANCE
• DISTRIBUTED
10/06/2024 R.SATHEESH , ASST.PROF, CSIT, MLRITM 13
• DYNAMIC
DATA TYPES IN JAVA
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
byte
• Byte data type is an 8-bit signed two's complement integer
• Minimum value is -128 (-2^7)
• Maximum value is 127 (inclusive)(2^7 -1)
• Default value is 0
• Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four
times smaller than an integer.
• Example: byte a = 100, byte b = -50
int
• Int data type is a 32-bit signed two's complement integer.
• Minimum value is - 2,147,483,648 (-2^31)
• Maximum value is 2,147,483,647(inclusive) (2^31 -1)
• Integer is generally used as the default data type for integral values unless there is a concern
about memory.
• The default value is 0
•10/06/2024
Example: int a = 100000, int b = -200000
R.SATHEESH , ASST.PROF, CSIT, MLRITM 17
long
• Long data type is a 64-bit signed two's complement integer
• Minimum value is -9,223,372,036,854,775,808(-2^63)
• Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
• This type is used when a wider range than int is needed
• Default value is 0L
• Example: long a = 100000L, long b = -200000L
float
• Float data type is a single-precision 32-bit IEEE 754 floating point
• Float is mainly used to save memory in large arrays of floating point numbers
• Default value is 0.0f
• Float data type is never used for precise values such as currency
• Example: float f1 = 234.5f
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
• A variable is a container which holds the value while the Java program is executed. A variable is assigned
with a data type.
• Variable is a name of memory location. There are three types of variables in java: local, instance and static.
• There are two types of data types in Java: primitive and non-primitive.
Int a = 10;
Int data type
a variable
Variable 10 value
Variable is name of reserved area allocated in memory. In other words, it is a name of memory location. It is a
other methods in the class aren't even aware that the variable exists.
It is called instance variable because its value is instance specific and is not shared among instances.
share among all the instances of the class. Memory allocation for static variable happens only once when the class is loaded in the
memory.
1.class Student{
2.int marks=50;//instance variable
3.static int total=100;//static variable
4.void method(){
5.int java=90;//local variable
6.}
7.}//end of class
• General convention for a variable’s scope is, it is accessible only within the block
in which it is declared. A block begins with a left curly brace { and ends with a
right curly brace }.
1.class Testarray3{
2.public static void main(String args[]){
3.//declaring and initializing 2D array
4.int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
5.//printing 2D array
6.for(int i=0;i<3;i++){
7. for(int j=0;j<3;j++){
8. System.out.print(arr[i][j]+" ");
9. }
10. System.out.println();
11.}
12.}
10/06/2024 R.SATHEESH , ASST.PROF, CSIT, MLRITM 36
13.}
OPERATORS IN JAVA
ARITHMETIC OPERATORS
RELATIONAL OPERATORS
BITWISE OPERATORS
LOGICAL OPERATORS
ASSIGNMENT OPERATORS
10/06/2024 R.SATHEESH , ASST.PROF, CSIT, MLRITM 37
ARITHMETIC OPERATORS
Operator Description Example
* (Multiplication) Multiplies values on either side of the operator. A * B will give 200
>= (greater than or Checks if the value of left operand is greater than or equal to (A >= B) is not true.
equal to) the value of right operand, if yes then condition becomes
true.
<= (less than or equal to) Checks if the value of left operand is less than or equal to
the value of right operand, if yes then condition becomes (A <= B) is true.
true.
10/06/2024 R.SATHEESH , ASST.PROF, CSIT, MLRITM 40
class RelationalOperators
{
public static void main(String[] args) {
int a = 7, b = 11; // create variables // value of a and b
System.out.println("a is “ + a + " and b is " + b);
System.out.println(a == b); // == operator // false
System.out.println(a != b); // != operator // true
System.out.println(a > b); // > operator // false
System.out.println(a < b); // < operator // true
System.out.println(a >= b); // >= operator // false
System.out.println(a <= b); // <= operator // true
} }
10/06/2024 R.SATHEESH , ASST.PROF, CSIT, MLRITM 41
BITWISE OPERATORS
Operator Description Example
& (bitwise and) Binary AND Operator copies a bit to the result if it (A & B) will give 12 which is
exists in both operands. 0000 1100
| (bitwise or) Binary OR Operator copies a bit if it exists in either (A | B) will give 61 which is 0011
operand. 1101
^ (bitwise XOR) Binary XOR Operator copies the bit if it is set in one (A ^ B) will give 49 which is 0011
operand but not both. 0001
(~A ) will give -61 which is 1100
~ (bitwise Binary Ones Complement Operator is unary and has the 0011 in 2's complement form due
compliment) effect of 'flipping' bits. to a signed binary number.
Binary Left Shift Operator. The left operands value
is moved left by the number of bits specified by the A << 2 will give 240
<< (left shift)
right operand. which is 1111 0000
Binary Right Shift Operator. The left operands value
is moved right by the number of bits specified by the A >> 2 will give 15 which is 1111
>> (right shift)
right operand.
Shift right zero fill operator. The left operands value
is moved right by the number of bits specified by the
>>> (zero fill right A >>>2 will give 15
right operand and shifted values are filled up with
shift)
10/06/2024 R.SATHEESH , ASST.PROF, CSIT, MLRITM which is 0000 1111 42
zeros.
LOGICAL OPERATORS
Operator Description Example
&& (logical Called Logical AND operator. If both the operands are non-zero, (A && B) is false
and) then the condition becomes true.
|| (logical or) Called Logical OR Operator. If any of the two operands are non- (A || B) is true
zero, then the condition becomes true.
! (logical Called Logical NOT Operator. Use to reverses the logical state of !(A && B) is true
not) its operand. If a condition is true then Logical NOT operator will
make false.
|=
10/06/2024 bitwise inclusive OR and assignment operator.
R.SATHEESH , ASST.PROF, CSIT, MLRITM C |= 2 is same as C = C 44
|2
JAVA EXPRESSIONS
A Java expression consists of variables, operators, literals, and method calls. To know more about method calls,
visit Java methods.
For example,
int score;
score = 90; Here, score = 90 is an statement that returns an int
Consider another example,
Double a = 2.2, b = 3.4, result;
result = a + b - 3.4; // here, a + b - 3.4 is an expression
Expression statements
We can convert an expression into a statement by terminating the expression with a ;. These are known as expression
statements. For example,
// expression
int num=a*b-c/d;
Or int num=10+20-5/2;
// statement
number = 10;
10/06/2024 R.SATHEESH , ASST.PROF, CSIT, MLRITM 45
CONTROL STATEMENTS
• Example:
The Java if-else statement 1. //A Java Program to demonstrate the use of if-
also tests the condition. It else statement.
2. //It is a program of odd and even number.
executes the if block if 3. public class IfElseExample {
5. }
10/06/2024 R.SATHEESH , ASST.PROF, CSIT, MLRITM 48
IF-ELSE-IF LADDER STATEMENT
The Java switch statement executes one statement public class SwitchExample {
from multiple conditions. It is like if-else-if ladder public static void main(String[] args) {
statement. //Declaring a variable for switch expression
SYNTAX: int number=20;
switch(expression){ //Switch expression
case value1: switch(number){
//code to be executed; //Case statements
case 10: System.out.println("10");
break; //optional
break;
case value2: case 20: System.out.println("20");
//code to be executed; break;
break; //optional case 30: System.out.println("30");
...... break;
//Default case statement
default:System.out.println("Not in
default: 10, 20 or 30");
code to be executed }
if all cases are }
}
not matched;
}10/06/2024 R.SATHEESH , ASST.PROF, CSIT, MLRITM 52
WHILE LOOP
class_name(){} }
id = i; }
The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or
class. We can change the access level of fields, constructors, methods, and class by applying the
access modifier on it.
1. Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
2. Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package.
If you do not specify any access level, it will be the default.
3. Protected: The access level of a protected modifier is within the package and outside the package through child class. If
you do not make the child class, it cannot be accessed from outside the package.
4. Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class,
within the package and outside the package.
Access Modifier within class within package outside package by outside package
subclass only
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public
10/06/2024 Y Y
R.SATHEESH Y
, ASST.PROF, CSIT, MLRITM Y 70
Example
class Ece_rocks{
private int data=40;
private void msg(){
System.out.println("Hello EEC Guys lets play with Java");}
}
public class Simple{
public static void main(String args[]){
Ece_rocks obj=new Ece_rocks();
System.out.println(obj.data);//Compile Time Error
obj.msg();//Compile Time Error
}
}
10/06/2024 R.SATHEESH , ASST.PROF, CSIT, MLRITM 71
THIS KEYWORD
Inheritance in Java is a mechanism in which one object acquires all the 1. class Animal{
properties and behaviors of a parent object.
Terms used in Inheritance: 2. void eat(){System.out.println("eating...");}
• Class: A class is a group of objects which have common properties. It is 3. }
a template or blueprint from which objects are created. 4. class Dog extends Animal{
• Sub Class/Child Class: Subclass is a class which inherits the other class.
It is also called a derived class, extended class, or child class.
5. void bark()
{System.out.println("barking...");}
• Super Class/Parent Class: Superclass is the class from where a subclass
inherits the features. It is also called a base class or a parent class. 6. }
• Reusability: As the name specifies, reusability is a mechanism which 7. class TestInheritance{
facilitates you to reuse the fields and methods of the existing class when
you create a new class. You can use the same fields and methods already
8. public static void main(String args[]){
defined in the previous class. 9. Dog d=new Dog();
• Syntax: 10.d.bark();
class Subclass-name extends Superclass-name
11.d.eat();
{
//methods and fields 12.}}
}
10/06/2024 R.SATHEESH , ASST.PROF, CSIT, MLRITM 77
OVERRIDING IN JAVA
// create 3 strings
String first = "java programming";
String second = "java programming";
String third = "python programming";
// compare first and second strings
boolean result1 = first.equals(second);
System.out.println("Strings first and second are equal: " + result1);
// compare first and third strings
boolean result2 = first.equals(third);
System.out.println("Strings first and third are equal: " + result2);
}}
Output:
Strings first and second are equal: true
10/06/2024 R.SATHEESH , ASST.PROF, CSIT, MLRITM 86
Strings first and third are equal: false