Java
Java
PROGRAMMING
AGENDA
Introduction
Comments
Class
Main function
JAVA Programming 3
INTRODUCTION
• Java is a class-based, object-oriented programming language that is
designed to have as few implementation dependencies as possible.
• It is intended to let application developers write once, and run
anywhere (WORA), meaning that compiled Java code can run on all
platforms that support Java without the need for recompilation.
• Java was first released in 1995 and is widely used for developing
desktop, web, and mobile applications.
• Java is known for its simplicity, robustness, and security features,
making it a popular choice for enterprise-level applications.
• Java is a class-based, object-oriented programming language and is
designed to have as few implementation dependencies as possible.
• Java applications are compiled to byte code that can run on any
Java Virtual Machine. The syntax of Java is similar to c/c++.
FEATURES OF JAVA
JAVA Programming 4
4. Robust: Java language is robust which means reliable. It is developed in such a way that it
puts a lot of effort into checking errors as early as possible, that is why the java compiler is
able to detect even those errors that are not easy to detect by another programming
language. The main features of java that make it robust are garbage collection, Exception
Handling, and memory allocation.
5. Secure: In java, we don’t have pointers, so we cannot access out-of-bound arrays i.e it
shows ArrayIndexOutOfBound Exception if we try to do so. That’s why several security
flaws like stack corruption or buffer overflow are impossible to exploit in Java. Also, java
programs run in an environment that is independent of the os(operating system) environment
which makes java programs more secure.
6. Distributed: We can create distributed applications using the java programming
language. Remote Method Invocation and Enterprise Java Beans are used for creating
distributed applications in java. The java programs can be easily distributed on one or more
systems that are connected to each other through an internet connection.
7. Multithreading: Java supports multithreading. It is a Java feature that allows concurrent
execution of two or more parts of a program for maximum utilization of the CPU.
8. Portable: As we know, java code written on one machine can be run on another machine.
The platform-independent feature of java in which its platform-independent bytecode can be
taken to any platform for execution makes java portable.
JAVA Programming
FEATURES OF JAVA 6
9. High Performance: Java architecture is defined in such a way that it reduces overhead
during the runtime and at some times java uses Just In Time (JIT) compiler where the compiler
compiles code on-demand basics where it only compiles those methods that are called making
applications to execute faster.
10. Dynamic flexibility: Java being completely object-oriented gives us the flexibility to add
classes, new methods to existing classes, and even create new classes through sub-classes.
Java even supports functions written in other languages such as C, C++ which are referred to
as native methods.
11. Sandbox Execution: Java programs run in a separate space that allows user to execute
their applications without affecting the underlying system with help of a bytecode verifier.
Bytecode verifier also provides additional security as its role is to check the code for any
violation of access.
12. Write Once Run Anywhere: As discussed above java application generates a ‘.class’ file
that corresponds to our applications(program) but contains code in binary format. It provides
ease t architecture-neutral ease as bytecode is not dependent on any machine architecture. It
is the primary reason java is used in the enterprising IT industry globally worldwide.
13. Power of compilation and interpretation: Most languages are designed with the
purpose of either they are compiled language or they are interpreted language. But java
integrates arising enormous power as Java compiler compiles the source code to bytecode and
JAVA PROGRAMMING
JAVA EXAMPLE 7
class Test
{
public static void main(String []args)
{
System.out.println(“My First Java Program.”);
}
}
JAVA PROGRAMMING
KEYWORDS 8
COMMENTS
Comments are ignored by the compiler but are useful to other programmers. The Java
programming language supports three kinds of comments:
/* text */
The compiler ignores everything from /* to */.
/** documentation */
This indicates a documentation comment (doc comment, for short). The compiler ignores this
kind of comment, just like it ignores comments that use /* and */. The javadoc tool
uses doc comments when preparing automatically generated documentation.
// text
The compiler ignores everything from // to the end of the line.
JAVA PROGRAMMING
JAVA VARIABLES 10
• Variables are the data containers that save the data values during
Java program execution. Every Variable in Java is assigned a data
type that designates the type and quantity of value it can hold. A
variable is a memory location name for the data.
• Variable declaration:
int count;
• Variable Initialization:
int count =10;
JAVA PROGRAMMING
TYPES OF VARIABLES 11
1. Local Variables
• A variable defined within a block or method or constructor is
called a local variable.
• These variables are created when the block is entered, or the
function is called and destroyed after exiting from the block or
when the call returns from the function.
• The scope of these variables exists only within the block in
which the variables are declared, i.e., we can access these
variables only within that block.
• Initialization of the local variable is mandatory before using it in
the defined scope.
JAVA PROGRAMMING
TYPES OF VARIABLES 12
2. Instance Variables:
• Instance variables are non-static variables and are declared in a class
outside of any method, constructor, or block.
• As instance variables are declared in a class, these variables are
created when an object of the class is created and destroyed when the
object is destroyed.
• Unlike local variables, we may use access specifiers for instance
variables. If we do not specify any access specifier, then the default
access specifier will be used.
• Initialization of an instance variable is not mandatory. Its default value
is dependent on the data type of variable. For String it
is null, for float it is 0.0f, for int it is 0, for Wrapper classes like Integer it
is null, etc.
• Instance variables can be accessed only by creating objects.
JAVA PROGRAMMING
TYPES OF VARIABLES 13
3. Static Variables
• Static variables are also known as class variables.
• These variables are declared similarly to instance variables. The
difference is that static variables are declared using the static
keyword within a class outside of any method, constructor, or
block.
• Unlike instance variables, we can only have one copy of a static
variable per class, irrespective of how many objects we create.
• Static variables are created at the start of program execution and
destroyed automatically when execution ends.
• Initialization of a static variable is not mandatory. Its default value
is dependent on the data type of variable. For String it is null,
for float it is 0.0f, for int it is 0, for Wrapper classes like Integer it
JAVA PROGRAMMING
DATA TYPES 14
Data types specify the different sizes and values that can
be stored in the variable. There are two types of data types
in Java:
characters representation of
‘a’, ‘\u0041’, ‘\101’, ‘\\’, ‘\’, ‘\
char Unicode character \u0000 16 bits
n’, ‘β’
ASCII values
0 to 255
-2,147,483,648
int twos-complement intger 0 32 bits -2,-1,0,1,2 to
2,147,483,647
-9,223,372,036,854,775,808
long twos-complement integer 0 64 bits -2L,-1L,0L,1L,2L to
9,223,372,036,854,775,807
1.23e100f , -1.23e-
float IEEE 754 floating point 0.0 32 bits
100f , .3f ,3.14F
upto 7 decimal digits
1.23456e300d , -123456e-
double IEEE 754 floating point 0.0 64 bits
300d , 1e1d
upto 16 decimal digits
JAVA PROGRAMMING
NON-PRIMITIVE OR 16
1. * : Multiplication
2. / : Division
3. % : Modulo
4. + : Addition
5. – : Subtraction
JAVA PROGRAMMING
UNARY OPERATORS 23
Unary operators need only one operand. They are used to increment,
decrement, or negate a value.
1. – : Unary minus, used for negating the values.
2. + : Unary plus indicates the positive value (numbers are positive without
this, however). It performs an automatic conversion to int when the type of its
operand is the byte, char, or short. This is called unary numeric promotion.
3. ++ : Increment operator, used for incrementing the value by 1. There are
two varieties of increment operators.
1. Post-Increment: Value is first used for computing the result and then
incremented.
2. Pre-Increment: Value is incremented first, and then the result is
computed.
4. – – : Decrement operator, used for decrementing the value by 1. There are
two varieties of decrement operators.
1. Post-decrement: Value is first used for computing the result and then
decremented.
2. Pre-Decrement: The value is decremented first, and then the result is
JAVA PROGRAMMING
ASSIGNMENT OPERATORS 24
• ‘=’ Assignment operator is used to assign a value to any variable. It has right-to-
left associativity, i.e. value given on the right-hand side of the operator is assigned
to the variable on the left, and therefore right-hand side value must be declared
before using it or should be a constant.
• The general format of the assignment operator is:variable = value;
• the assignment operator can be combined with other operators to build a shorter
version of the statement called a Compound Statement. For example, instead
of a = a+5, we can write a += 5.
1. +=, for adding the left operand with the right operand and then assigning it to the
variable on the left.
2. -=, for subtracting the right operand from the left operand and then assigning it to the
variable on the left.
3. *=, for multiplying the left operand with the right operand and then assigning it to the
variable on the left.
4. /=, for dividing the left operand by the right operand and then assigning it to the
variable on the left.
5. %=, for assigning the modulo of the left operand by the right operand and then
assigning it to the variable on the left.
JAVA PROGRAMMING
RELATIONAL OPERATORS 25
• Relational operators are used to check for relations like equality, greater than,
and less than. They return boolean results after the comparison and are
extensively used in looping statements as well as conditional if-else
statements.
• Relational operators are-
1. ==, Equal to returns true if the left-hand side is equal to the right-hand
side.
2. !=, Not Equal to returns true if the left-hand side is not equal to the
right-hand side.
3. <, less than: returns true if the left-hand side is less than the right-
hand side.
4. <=, less than or equal to returns true if the left-hand side is less than
or equal to the right-hand side.
5. >, Greater than: returns true if the left-hand side is greater than the
right-hand side.
6. >=, Greater than or equal to returns true if the left-hand side is
JAVA PROGRAMMING
LOGICAL OPERATORS 26
• These operators are used to perform “logical AND” and “logical OR”
operations, i.e., a function similar to AND gate and OR gate in digital
electronics.
• One thing to keep in mind is the second condition is not evaluated if
the first one is false, i.e., it has a short-circuiting effect. Used
extensively to test for several conditions for making a decision. Java
also has “Logical NOT”, which returns true when the condition is
false and vice-versa
1.&&, Logical AND: returns true when both conditions are true.
2.||, Logical OR: returns true if at least one condition is true.
3.!, Logical NOT: returns true when a condition is false and
vice-versa
JAVA PROGRAMMING
TERNARY OPERATOR 27
These operators are used to shift the bits of a number left or right,
thereby multiplying or dividing the number by two, respectively. They
can be used when we have to multiply or divide a number by two.
General format- number shift_op
number_of_places_to_shift;
<<, Left shift operator: shifts the bits of the number to the left and
fills 0 on voids left as a result. Similar effect as multiplying the number
with some power of two.
>>, Signed Right shift operator: shifts the bits of the number to
the right and fills 0 on voids left as a result. The leftmost bit depends
on the sign of the initial number. Similar effect to dividing the number
with some power of two.
>>>, Unsigned Right shift operator: shifts the bits of the number
JAVA PROGRAMMING
INSTANCEOF OPERATOR 30
General format-
ASSOCIATIVITY
• Precedence and associative rules are used when dealing with hybrid
equations involving more than one type of operator. In such cases,
these rules determine which part of the equation to consider first, as
there can be many different valuations for the same equation.
JAVA PROGRAMMING
PRECEDENCE AND 32
ASSOCIATIVITY
THANK YOU