0% found this document useful (0 votes)
4 views33 pages

Java

Java is a class-based, object-oriented programming language known for its platform independence, simplicity, robustness, and security features, allowing developers to write once and run anywhere. Key features include multithreading, dynamic flexibility, and a variety of data types, including primitive and non-primitive types. Java's structure includes classes, methods, variables, and operators, making it suitable for a wide range of applications from desktop to enterprise-level solutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views33 pages

Java

Java is a class-based, object-oriented programming language known for its platform independence, simplicity, robustness, and security features, allowing developers to write once and run anywhere. Key features include multithreading, dynamic flexibility, and a variety of data types, including primitive and non-primitive types. Java's structure includes classes, methods, variables, and operators, making it suitable for a wide range of applications from desktop to enterprise-level solutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

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

1. Platform Independent: Compiler converts source code to bytecode and then


the JVM executes the bytecode generated by the compiler. This bytecode can run on
any platform be it Windows, Linux, or macOS which means if we compile a program
on Windows, then we can run it on Linux and vice versa. Each operating system has
a different JVM, but the output produced by all the OS is the same after the execution
of the bytecode. That is why we call java a platform-independent language.
2. Object-Oriented Programming Language: Organizing the program in the
terms of a collection of objects is a way of object-oriented programming, each of
which represents an instance of the class.
The four main concepts of Object-Oriented programming are:
Abstraction
Encapsulation
Inheritance
Polymorphism
3. Simple: Java is one of the simple languages as it does not have complex features
like pointers, operator overloading, multiple inheritances, and Explicit memory
allocation.
JAVA Programming
FEATURES OF JAVA 5

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

• class : class keyword is used to declare classes in Java


• public : It is an access specifier. Public means this function is visible to
all.
• static : static is again a keyword used to make a function static. To
execute a static function you do not have to create an Object of the
class. The main() method here is called by JVM, without creating any
object for class.
• void : It is the return type, meaning this function will not return
anything.
• main : main() method is the most important method in a Java program.
This is the method which is executed, hence all the logic must be inside
the main() method. If a java class is not having a main() method, it
causes compilation error.
• String[] args : This is used to signify that the user may opt to enter
JAVA Programming 9

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:

1.Primitive data types: The primitive data types include


boolean, char, byte, short, int, long, float and double

2.Non-primitive data types: The non-primitive data types


include Classes, Interfaces, and Arrays.
JAVA PROGRAMMING
PRIMITIVE DATA TYPES 15

Type Description Default Size Example Literals Range of values


boolean true or false false 1 bit true, false true, false

byte twos-complement integer 0 8 bits (none) -128 to 127

characters representation of
‘a’, ‘\u0041’, ‘\101’, ‘\\’, ‘\’, ‘\
char Unicode character \u0000 16 bits
n’, ‘β’
ASCII values
0 to 255

short twos-complement integer 0 16 bits (none) -32,768 to 32,767

-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

REFERENCE DATA TYPES


The Reference Data Types will contain a memory address of variable values
because the reference types won’t store the variable value directly in
memory. They are strings, objects, arrays, etc.
1. Strings
Strings are defined as an array of characters. The difference between a character array
and a string in Java is, that the string is designed to hold a sequence of characters in a
single variable whereas, a character array is a collection of separate char-type entities.
Unlike C/C++, Java strings are not terminated with a null character.
Syntax: Declaring a string
<String_Type> <string_variable> = “<sequence_of_string>”;
Example:
// Declare String without using new operator String s = “Hello";
// Declare String using new operator String s1 = new String(“Hello");
JAVA PROGRAMMING
NON-PRIMITIVE OR 17

REFERENCE DATA TYPES


2. Class
A class is a user-defined blueprint or prototype from which objects are
created. It represents the set of properties or methods that are common
to all objects of one type. In general, class declarations can include these
components, in order:
1.Modifiers: A class can be public or has default access. Refer to
access specifiers for classes or interfaces in Java
2.Class name: The name should begin with an initial letter (capitalized
by convention).
3.Superclass(if any): The name of the class’s parent (superclass), if
any, preceded by the keyword extends. A class can only extend
(subclass) one parent.
4.Interfaces(if any): A comma-separated list of interfaces
implemented by the class, if any, preceded by the keyword implements.
A class can implement more than one interface.
JAVA PROGRAMMING
NON-PRIMITIVE OR 18

REFERENCE DATA TYPES


3. Object
An Object is a basic unit of Object-Oriented
Programming and represents real-life entities. A typical
Java program creates many objects, which as you know,
interact by invoking methods. An object consists of :
1.State: It is represented by the attributes of an object.
It also reflects the properties of an object.
2.Behavior: It is represented by the methods of an
object. It also reflects the response of an object to other
objects.
3.Identity: It gives a unique name to an object and
JAVA PROGRAMMING
NON-PRIMITIVE OR 19

REFERENCE DATA TYPES


4. Interface
Like a class, an interface can have methods and variables, but the
methods declared in an interface are by default abstract (only
method signature, no body).
• Interfaces specify what a class must do and not how. It is the
blueprint of the class.
• An Interface is about capabilities like a Player may be an
interface and any class implementing Player must be able to (or
must implement) move(). So it specifies a set of methods that
the class has to implement.
• If a class implements an interface and does not provide method
bodies for all functions specified in the interface, then the class
must be declared abstract.
JAVA PROGRAMMING
NON-PRIMITIVE OR 20

REFERENCE DATA TYPES


5. Array
An Array is a group of like-typed variables that are referred to by a common
name. Arrays in Java work differently than they do in C/C++. The following
are some important points about Java arrays.
• In Java, all arrays are dynamically allocated.
• 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.
• Java array can also be used as a static field, a local variable, or a method
parameter.
• The size of an array must be specified by an int value and not long or
short.
JAVA PROGRAMMING
OPERATORS 21

• Operators in Java are the symbols used for performing


specific operations in Java. Operators make tasks like
addition, multiplication, etc
• There are multiple types of operators in Java all are
mentioned below:
1.Arithmetic Operators 6.Ternary Operator
2.Unary Operators 7.Bitwise Operators
3.Assignment Operator 8.Shift Operators
4.Relational Operators 9.instance of operator
5.Logical Operators
JAVA PROGRAMMING
ARITHMETIC OPERATORS 22

• They are used to perform simple arithmetic operations on


primitive data types.

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

• The ternary operator is a shorthand version of the if-else


statement. It has three operands and hence the name
Ternary.

• The general format is:


• condition ? if true : if false

• The above statement means that if the condition evaluates


to true, then execute the statements after the ‘?’ else
execute the statements after the ‘:’.
JAVA PROGRAMMING
BITWISE OPERATORS 28

• These operators are used to perform the manipulation of individual


bits of a number. They can be used with any of the integer types.
They are used when performing update and query operations of the
Binary indexed trees.
1. &, Bitwise AND operator: returns bit by bit AND of input
values.
2. |, Bitwise OR operator: returns bit by bit OR of input values.
3. ^, Bitwise XOR operator: returns bit-by-bit XOR of input
values.
JAVA PROGRAMMING
SHIFT OPERATORS 29

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

The instance of the operator is used for type checking. It can


be used to test if an object is an instance of a class, a
subclass, or an interface.

General format-

object instance of class/subclass/interface


JAVA PROGRAMMING
PRECEDENCE AND 31

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

You might also like