java unit 1 NOTES
java unit 1 NOTES
• What is oops?
• Difference between c and java?
• Oops concepts-
class,object,Encapsulation,Inheritance,Polymorphism,Dynamic
Binding
• History of java
• Java features
• Java Environment-JDK,JRE,JVM,API
• Tokens,Identifiers,Constants,keywords
Basic concepts of OOPS programming
❑
Class is a template or blueprint used to create objects and to
define object data types and methods.
A class is a group of objects which have common properties.
A class — in the context of Java — is a
template used to create objects and to
define object data types and methods.
Classes are categories, and objects are items
within each category. All class objects should
have the basic class properties.
}
Object
An object is a real world entity.
An object is an instance of a class.
Multithreading
Simple
Distributed
The JIT compiler helps improve the performance of Java programs by compiling
bytecodes into native machine code at run time.
Machine code ,compiling it just in time to run.
Dynamic
Java is considered to be more dynamic than C or C++ since it is designed to adapt
to an evolving environment. Java programs can carry an extensive amount of run-
time information that can be used to verify and resolve accesses to objects at run-
time.
❑ Java Environment
• JDK –Java Development Kit- collection
of tools for developing and running the
java programs.
• JSL-Java Standard Library Contains
classes and methods also called API
(Application programming Interface)
• JVM-Java virtual Machine provides a
runtime environment in which we can
execute bytecode.
• JRE-java runtime environmemt-
collection of tools (developmemt of
applications and runtime
environment).JVM is a part of JRE.
• JIT –just in time complier is a code
generator that converts java bytecode 1.JDK is for development purposes whereas JRE is for running
into machine language instructions. the java programs.
2.JDK and JRE both contain JVM so that we can run our java
JVM is called virtual because it program.
provides an interface that does not 3.JVM is the heart of Java programming language and provides
depend on the underlying operating platform independence.
system and machine hardware.
What are the Java tokens?
Java tokens are the basic building
blocks of a Java program. They are
the smallest units of a program,
and they include keywords,
identifiers, operators, literals,
separators, and comments.
Separators are symbols that
separate different parts of a program.
Primitive data types in Java are those data types whose variables
can store only one value at a time. We cannot store multiple
values of the same type. These data types are pre-defined in Java.
They are named by a Keyword.
Non-primitive data types are created by programmer. It
is also called as ‘Reference data types’ because it refers a
memory location where data is stored. It is also called as
user defined datatypes or derived datatypes.
Primitive datatypes declaration in java
char Data Type in Java
In Java, the data type used to store characters
is char. A character data type represents a single
character.
A character is an identifier which is enclosed
within single quotes. In java to represent character
data, we use a data type called char.
This data type takes two byte (16-bit) since it
follows Unicode character set.
char letter = 'A’;
Example:
• String string2 = new String("This is a Java class "); //declaring string using new
operator
• System.out.println(string1);
• System.out.println(string2);
• }
• }
• Output:
• Hello World
This is a Java Tutorial
• Arrays: Java array is an object which contains elements of a similar
data type. Arrays store one or more values of a specific data type and
provide indexed access to store the same.
The valid syntax for array The valid syntax for array initialization can be:
declaration can be: array_name = new data-type [size of array];
data-type array_name [ ]; array_name = new data-type {elements of array using
data-type [ ] array_name; using commas};
• public class Arraydemo
•{
• public static void main(String args[])
•{
• int [ ] marksOfStudents = new int[ ] {65, 90, 78, 60, 84 };
• System.out.println("Marks of first student: " +marksOfStudents[0]);
• System.out.println("Marks of second student: "
+marksOfStudents[1]);
• System.out.println("Marks of third student: " +marksOfStudents[2]);
• System.out.println("Marks of fourth student: " +marksOfStudents[3]);
• System.out.println("Marks of fifth student: " +marksOfStudents[4]);
•}
•}
• Marks of first student: 65
Marks of second student: 90
Marks of third student: 78
Marks of fourth student: 60
Marks of fifth student: 84
• Classes: A class is a collection of objects of the same type. It is a
user-defined blueprint or prototype which defines the behavior or
state of objects. A class contains fields(variables) and methods to
describe the behavior of an object.
• We create a class using a class keyword.
• A class can be declared using the following components in the order-
• 1. Access modifiers: Access modifiers define the access privileges of a
class. A class can be public or it has default access.
• 2. Class name: The name of a class should represent a noun and must
start with a capital letter. These are the best practices to be kept in
mind while declaring any class.
• 3. Body: The class body contains properties and methods. The body is
always enclosed by curly braces { }.
• Syntax of writing a class:
• AccessModifier class class_name
• {
• Class body - variables and methods();
• }
• System.out.println("Marks of student: " +student1.marks); //Accessing the property “marks” of the class with the help of an object.
• }
• }
• Output:
• Marks of student: 76
• An interface behaves like a blueprint of a class,
which specifies “what a class has to do and not how
it will do”.
• Interface: Like a class, an interface can have
methods and variables, but the methods
declared in interface are by default abstract (only
method signature, no body).
• The Math.max(x,y) method can be used to find the highest value of x and y:
• Math.max(5, 10);
Math.min(5, 10);
Math.sqrt(x)
The Math.sqrt(x) method returns the square root of x:
Math.sqrt(64);
1. public class JavaMathExample1
2. {
3. public static void main(String[] args)
4. {
5. double x = 28;
6. double y = 4;
7.
8. // return the maximum of two numbers
9. System.out.println("Maximum number of x and y is: " +Math.max(x, y));
10.
11. // return the square root of y
12. System.out.println("Square root of y is: " + Math.sqrt(y));
13.
14. //returns 28 power of 4 i.e. 28*28*28*28
15. System.out.println("Power of x and y is: " + Math.pow(x, y));
16.
17.
1.// return the logarithm of given value
2. System.out.println("Logarithm of x is: " + Math.log(x));
3. System.out.println("Logarithm of y is: " + Math.log(y));
4.
5. // return the logarithm of given value when base is 10
6. System.out.println("log10 of x is: " + Math.log10(x));
7. System.out.println("log10 of y is: " + Math.log10(y));
8.
9. // return a power of 2
10. System.out.println("exp of a is: " +Math.exp(x));
11.
12. }
13.}
Operators in java
• What are the Java Operators?
Arithimetic Operators:
• Arithmetic operators are used to perform
arithmetic operations on variables and data.
• 1. Addition(+): This operator is a binary
operator and is used to add two operands.
• num1 + num2
Example:
num1 = 10, num2 = 20 ;
sum = num1 + num2 ;
= 30
• 2. Subtraction(-): This operator is a binary operator and is used to subtract
two operands.
• Syntax:
num1 - num2
• Example:
num1 = 20, num2 = 10
sub = num1 - num2 = 10
Example:
num1 = 20, num2 = 10 mult = num1 * num2 = 200
• 4. Division(/): This is a binary operator that is used to divide the
first operand(dividend) by the second operand(divisor) and give the
quotient as a result.
• num1 / num2
• num1 = 20, num2 = 10 div = num1 / num2 = 2
• 5. Modulus(%): This is a binary operator that is used to return the
remainder when the first operand(dividend) is divided by the
second operand(divisor).
• Syntax:
• num1 % num2
• num1 = 5, num2 = 2 mod = num1 % num2 = 1
• public class ArithmeticOperator
• {
• public static void main(String[] args) Addition num1+num2 13
• { Subtraction num1-num2 -3
• int add, Multiplication num1*num2 40
• sub, Division num1/num2 0
• mul, Modulus num2%num1 3
• div,
• mod;
• int num1 = 5,
• num2 = 8;
• add = num1 + num2;
• sub = num1 - num2;
• mul = num1 * num2;
• div = num1 / num2;
• mod = num2 % num1;
• System.out.println("Addition num1+num2 " + add);
• System.out.println("Subtraction num1-num2 " + sub);
• System.out.println("Multiplication num1*num2 " + mul);
• System.out.println("Division num1/num2 " + div);
• System.out.println("Modulus num2%num1 " + mod);
• }
2. Relation Operators
• Relational operators are used to check the relationship between two
operands.
• Relational operators are used in decision making and loops.
Logical Operators
• Logical operators are used to
perform logical “AND”, “OR” and
“NOT” operations.
• 1. Logical ‘AND’ Operator (&&)
• This operator returns true when
both the conditions under
consideration are satisfied or are
true. If even one of the two yields public class Land {
false, the operator results false. public static void main(String[] args) {
In Simple terms, cond1 && int x = 5;
System.out.println(x > 3 && x < 10); // returns true because
cond2 returns true when both is greater than 3 AND 5 is less than 10
cond1 and cond2 are true (i.e. }
non-zero). }
Syntax:
condition1 || condition2
The left side operand of the assignment operator is a variable and the right side operand of the assignment
operator is a value.
variable operator value;
Increment and Decrement Operators:
The increment (++) and decrement operator (--) are simply used to increase and decrease the value by one.
Increment and decrement operators are unary operators. We can only apply these operators on a single operand,
hence these operators are called as unary operators.
In this example, we have created the two packages pack and mypack. The A class
of pack package is public, so can be accessed from outside the package. But msg
method of this package is declared as protected, so it can be accessed from
outside the class only through inheritance.
• Default
• If you don't use any modifier, it is treated as default by default. The
default modifier is accessible only within package.
• It cannot be accessed from outside the package. //save by B.java
package mypack;
import pack.*;
• /save by A.java class B{
public static void main(String args[]){
• package pack;
A obj = new A();//Compile Time Error
• class A{ obj.msg();//Compile Time Error
• void msg(){ }
}
• System.out.println("Hello");
• }
• }