Java 23 Reg Unit1 Notes
Java 23 Reg Unit1 Notes
Object
Any entity that has state and behavior is known as an object. For
example, a chair, pen, table, keyboard, bike, etc. It can be physical or
logical.
An Object can be defined as an instance of a class. An object contains an
address and takes up some space in memory. Objects can communicate
without knowing the details of each other's data or code. The only
necessary thing is the type of message accepted and the type of response
returned by the objects.
Example: A dog is an object because it has states like color, name, breed,
etc. as well as behaviors like wagging the tail, barking, eating, etc.
ADVERTISEMENT
ADVERTISEMENT
Class
Collection of objects is called class. It is a logical entity.
A class can also be defined as a blueprint from which you can create an
individual object. Class doesn't consume any space.
Inheritance
When one object acquires all the properties and behaviors of a parent
object, it is known as inheritance. It provides code reusability. It is used to
achieve runtime polymorphism.
Polymorphism
If one task is performed in different ways, it is known as polymorphism.
For example: to convince the customer differently, to draw something, for
example, shape, triangle, rectangle, etc.
Abstraction
Hiding internal details and showing functionality is known as abstraction.
For example phone call, we don't know the internal processing.
ADVERTISEMENT
ADVERTISEMENT
Encapsulation
Binding (or wrapping) code and data together into a single unit are known
as encapsulation. For example, a capsule, it is wrapped with different
medicines.
Benefits of OOPs
1. Modularity
2. Reusability
3. Better Code Organization
4. Maintenance
5. Collaborative Development
6. Code reuse
7. Scalability
8. Real-World Modeling
Application of OOPs
Genesis of Java
The history of Java starts with the Green Team. Java team members
(also known as Green Team), initiated this project to develop a
language for digital devices such as set-top boxes, televisions, etc.
However, it was best suited for internet programming. Later, Java
technology was incorporated by Netscape.
The principles for creating Java programming were "Simple, Robust,
Portable, Platform-independent, Secured, High Performance,
Multithreaded, Architecture Neutral, Object-Oriented, Interpreted,
and Dynamic". Java was developed by James Gosling, who is known
as the father of Java, in 1995. James Gosling and his team members
started the project in the early '90s.
Example:
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main (String[] args) {
System.out.println("GFG!");
}
}
Output
GFG!
The above-written code is called JAVA source code.
The compiler compiles the source code.
Finally, Interpreter executes the compiled source code.
Whenever we write any program, it is not written in
machine code. We write it in a high-level language like
JAVA, C++, Python, etc. But the computer understands only
the machine code. So when we execute our program, it is
first converted into machine code or Byte code by the
compiler and then executed by the Interpreter.
This intermediate code or the byte can run on any
platform making, JAVA a platform-independent
language.
JAVA BUZZWORDS
Solution: To get around this problem, give your variables, classes, and
methods names that are relevant and descriptive that do not conflict
with reserved terms. Use an Integrated Development Environment
(IDE) that offers code assistance if you're unsure whether a word is a
reserved keyword or reference the Java documentation.
Case Sensitivity
The identifiers "myVariable" and "myvariable" are processed differently
in Java because of the case-sensitivity of the language. It can be more
difficult to read and maintain your code if the cases are mixed together
inconsistently and can result in subtle problems.
Solution: Use comments to explain and set the stage for your code. To
avoid using too many comments, use names for variables and methods
that make sense. To improve readability, properly indent your code
and keep a consistent coding style.
String Escapes
Special characters like newline ('n') and double quotes ('"') may be
required when creating strings in Java. Syntax issues can occur when
using these characters without the correct escapes.
Solution: For a workaround, represent special characters in strings
using escape sequences. Use """ for a double quotation inside of a
string, for instance, and "n" for a newline character. By doing this, you
can be certain that the Java compiler will understand these characters
properly.
ADVERTISEMENT
Data types in Java are of different sizes and values that can be
stored in the variable that is made as per convenience and
circumstances to cover up all test cases. Java has two categories in
which data types are segregated
1. Primitive Data Type: such as Boolean, char, int, short, byte, long,
float, and double
Primitive data are only single values and have no special capabilities.
There are 8 primitive data types. They are depicted below in tabular
format below as follows:
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
1. Strings
Example:
String s = "GeeksforGeeks";
2.Array
Since arrays are objects in Java, we can find their length using
member length. This is different from C/C++ where we find length
using size.
A Java array variable can also be declared like other variables with []
after the data type.
The variables in the array are ordered and each has an index
beginning with 0.
The size of an array must be specified by an int value and not long
or short.
Variables in Java
Variable Initialization
1. Local Variables
2. Instance Variables
3. Static Variables
1. Local Variables
2. Instance Variables
3. Static Variables
Operators in Java
There are many types of operators in Java which are given below:
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.
equality == !=
bitwise ^
exclusive OR
bitwise |
inclusive OR
logical OR ||
Ternary ternary ?:
The Java unary operators require only one operand. Unary operators
are used to perform various operations i.e.:
int x=10;
System.out.println(x++);//10 (11)
System.out.println(++x);//12
System.out.println(x--);//12 (11)
System.out.println(--x);//10
}}
Output:
10
12
12
10
Example 2: ++ and --
int a=10;
int b=10;
System.out.println(a++ + ++a);//10+12=22
System.out.println(b++ + b++);//10+11=21
}}
Output:
22
21
Example: ~ and !
int a=10;
int b=-10;
boolean c=true;
boolean d=false;
System.out.println(!d);//true
}}
Output:
-11
false
true
Example
int a=10;
int b=5;
System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0
}}
Output:
15
50
System.out.println(10*10/5+3-1*4/2);
}}
Output:
21
The Java left shift operator << is used to shift all of the bits in a
value to the left side of a specified number of times.
System.out.println(10<<2);//10*2^2=10*4=40
System.out.println(10<<3);//10*2^3=10*8=80
System.out.println(20<<2);//20*2^2=20*4=80
System.out.println(15<<4);//15*2^4=15*16=240
}}
Output:
40
80
80
240
The Java right shift operator >> is used to move the value of the left
operand to right by the number of bits specified by the right
operand.
public OperatorExample{
System.out.println(10>>2);//10/2^2=10/4=2
System.out.println(20>>2);//20/2^2=20/4=5
System.out.println(20>>3);//20/2^3=20/8=2
}}
Output:
System.out.println(20>>2);
System.out.println(20>>>2);
System.out.println(-20>>2);
System.out.println(-20>>>2);
}}
Output:
-5
1073741819
The logical && operator doesn't check the second condition if the
first condition is false. It checks the second condition only if the first
one is true.
The bitwise & operator always checks both conditions whether first
condition is true or false.
int a=10;
int b=5;
int c=20;
}}
Output:
false
false
int a=10;
int b=5;
int c=20;
1. }}
Output:
false
10
false
11
The logical || operator doesn't check the second condition if the first
condition is true. It checks the second condition only if the first one
is false.
int a=10;
int b=5;
int c=20;
//|| vs |
}}
Output:
true
true
true
10
true
11
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}}
Output:
Another Example:
int a=10;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}}
Output:
int a=10;
int b=20;
a+=4;//a=a+4 (a=10+4)
b-=4;//b=b-4 (b=20-4)
System.out.println(a);
System.out.println(b);
}}
Output:
14
16
int a=10;
a+=3;//10+3
System.out.println(a);
a-=4;//13-4
System.out.println(a);
a*=2;//9*2
System.out.println(a);
a/=2;//18/2
System.out.println(a);
}}
ADVERTISEMENT
ADVERTISEMENT
Output:
13
9
18
short a=10;
short b=10;
System.out.println(a);
}}
Output:
short a=10;
short b=10;
System.out.println(a);
}}
Output:
20