0% found this document useful (0 votes)
25 views

Java Basics 1 - Chapter One

Uploaded by

dilankasilva86
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Java Basics 1 - Chapter One

Uploaded by

dilankasilva86
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

JAVA BASICS-I

Chapter 1

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


What is Java?

Java is a general-purpose, class-based, object-oriented programming language designed


for having lesser implementation dependencies. It is a computing platform for application
development. Java is fast, secure, and reliable, therefore. It is widely used for developing
Java applications in laptops, data centers, game consoles, scientific supercomputers, cell
phones, etc.

What is Java Platform?

Java Platform is a collection of programs that help programmers to develop and run Java
programming applications efficiently. It includes an execution engine, a compiler, and a set
of libraries in it. It is a set of computer software and specifications. James Gosling
developed the Java platform at Sun Microsystems, and the Oracle Corporation later
acquired it.

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


History of Java
James Gosling initiated Java language project in June 1991 for use in one of his many set-top box
projects. The language, initially called ‘Oak’ after an oak tree that stood outside Gosling's office, also
went by the name ‘Green’ and ended up later being renamed as Java, from a list of random words.

Sun released the first public implementation as Java 1.0 in 1995. It promised Write Once, Run
Anywhere (WORA), providing no-cost run-times on popular platforms.

On 13 November 2006, Sun released much of Java as free and open-source software under the terms
of the GNU General Public License (GPL).

On 8 May 2007, Sun finished the process, making all of Java's core code free and open-source, aside
from a small portion of code to which Sun did not hold the copyright.

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


Java Features

• Simple

Java is very easy to learn, and its syntax is simple, clean and easy to understand. According to Sun
Microsystem, Java language is a simple programming language because:
• Java syntax is based on C++ (so easier for programmers to learn it after C++).
• Java has removed many complicated and rarely-used features, for example, explicit pointers,
operator overloading, etc.
• There is no need to remove unreferenced objects because there is an Automatic Garbage
Collection in Java.

• Object Oriented

Java is an object-oriented programming language. Everything in Java is an object. Object-oriented


means we organize our software as a combination of different types of objects that incorporate both
data and behavior.
Object-oriented programming (OOPs) is a methodology that simplifies software development and
maintenance by providing some rules.

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


• Platform Independent

Java is platform independent because it is different from other languages like C, C++, etc. which are
compiled into platform specific machines while Java is a write once, run anywhere language. A
platform is the hardware or software environment in which a program runs.
There are two types of platforms software-based and hardware-based. Java provides a software-
based platform.
The Java platform differs from most other platforms in the sense that it is a software-based platform
that runs on top of other hardware-based platforms. It has two components:

1.Runtime Environment
2.API(Application Programming Interface)

• Portable

Java is portable because it facilitates you to carry the Java bytecode to any platform. It
doesn't require any implementation.

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


• High Performance

Java is faster than other traditional interpreted programming languages because Java bytecode is "close"
to native code. It is still a little bit slower than a compiled language (e.g., C++). Java is an interpreted
language that is why it is slower than compiled languages, e.g., C, C++

• Multi Threaded

A thread is like a separate program, executing concurrently. We can write Java programs that deal with
many tasks at once by defining multiple threads. The main advantage of multi-threading is that it doesn't
occupy memory for each thread. It shares a common memory area. Threads are important for multi-media,
Web applications, etc.

• Architecture Neutral

Java is architecture neutral because there are no implementation dependent features, for example, the size
of primitive types is fixed.
In C programming, int data type occupies 2 bytes of memory for 32-bit architecture and 4 bytes of memory
for 64-bit architecture. However, it occupies 4 bytes of memory for both 32 and 64-bit architectures in Java.
• Secured

Java is best known for its security. With Java, we can develop virus-free systems. Java is secured
because:

•No explicit pointer


•Java Programs run inside a virtual machine sandbox

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


What is JVM - JRE - JDK

JVM (Java Virtual Machine)

• JVM (Java Virtual Machine) is an abstract machine.


• It does not physically exist.
• It is a specification that provides runtime environment in
which java bytecode can be executed.

The JVM performs following operation:


•Loads code
•Verifies code
•Executes code
•Provides runtime environment

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


JRE (Java Run-time Environment )
Java Run-time Environment (JRE) is the part of the Java Development Kit (JDK). It is a freely available
software distribution which has Java Class Library, specific tools, and a stand-alone JVM. It is the most
common environment available on devices to run java programs. The source Java code gets compiled and
converted to Java bytecode. If you wish to run this bytecode on any platform, you require JRE. The JRE
loads classes, verify access to memory, and retrieves the system resources. JRE acts as a layer on the top
of the operating system.
JDK (Java Development Kit)
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software
development environment which is used to develop java applications and applets. It physically exists. It
contains JRE + development tools.

JDK is an implementation of any one of the below given Java Platforms released by Oracle corporation:

•Standard Edition Java Platform


•Enterprise Edition Java Platform
•Micro Edition Java Platform

The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an
interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc) etc.
to complete the development of a Java Application.

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


Java Compiler

Java source code is compiled into bytecode when we use the javac compiler. The bytecode gets saved on
the disk with the file extension .class. When the program is to be run, the bytecode is converted, using the
just-in-time (JIT) compiler. The result is machine code which is then fed to the memory and is executed.

Java code needs to be compiled twice in order to be executed:

1. Java programs need to be compiled to bytecode.


2. When the bytecode is run, it needs to be converted to machine code.

The Java classes/bytecode are compiled to machine code and loaded into memory by the JVM when
needed the first time. This is different from other languages like C/C++ where programs are to be compiled
to machine code and linked to create an executable file before it can be executed.

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


What Happens to your code ?

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


Let's set up the environment
To start coding with Java

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


Let’s Do the very first coding in Java

1. Write a program to display “ Hello World! ”


2. Write a program to display your name, age and hobbies.

//A Very Simple Example


class MyFirstProgram {
public static void main(String[] args){
System.out.println(“Hello World!");
}
}

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


Java Variables
A variable is the name of a reserved area allocated in memory. In other words, it is a name of the memory
location. It is a combination of "vary + able" which means its value can be changed.

Reserved Areas

10 A
Types of Variables
“Java”
There are three types of variables in Java:

1. local variable
RAM (Random Access 2. instance variable
Memory)
3. static variable

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


1) Local Variable

A variable declared inside the body of the method is called local variable. You can use this variable only within that
method and the other methods in the class aren't even aware that the variable exists.

A local variable cannot be defined with "static" keyword.

2) Instance Variable

A variable declared inside the class but outside the body of the method, is called an instance variable. It is not declared
as static.

It is called an instance variable because its value is instance-specific and is not shared among instances.

3) Static variable

A variable that is declared as static is called a static variable. It cannot be local. You can create a single copy of the static
variable and share it among all the instances of the class. Memory allocation for static variables happens only once
when the class is loaded in the memory.

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


public class VariableTypes
{
int data1=50;//instance variable
static int m=100;//static variable

void method()
{
int n=90;//local variable
}
public static void main(String args[])
{
int data=50;//Local variable
}
}//end of class

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


Data Types
Data types specify the different sizes and values that can be stored in the variable.
There are two types of data types in Java:

Primitive data types: The primitive data types include boolean, char, byte, short,
int, long, float and double.

Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays.

There are 8 types of primitive data types:

1. boolean data type


2. byte data type
3. char data type
4. short data type
5. int data type
6. long data type
7. float data type
8. double data type

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE
JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE
Widening and Narrowing in java

Widening

Widening is a process in which a smaller type is converted to wider/larger type. This is an


implicit/automatic conversion, Java itself does this for us. This is also known as Upcasting.
Automatic conversion of a subclass reference variable to a superclass reference variable is also
part of Widening.

Byte → Short → Int → Long → Float → Double


Subclass → Superclass
Widening or Implicit Conversion

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


Narrowing

Narrowing is a process in which a larger type is converted to smaller type. This needs to be done
explicitly by the programmer. This is also known as Downcasting. Explicit conversion of a
superclass reference variable to a subclass reference variable is also part of Narrowing.

Double → Float → Long → Int → Short → Byte


Superclass → Subclass
Narrowing or Explicit Conversion

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


class WideningAndNarrowing
{
public static void main(String [] args)
{
byte b=20;
int i=b; //byte to int widening
long l=b; //byte to long widening
double d=b; //byte to double widening
System.out.println("int value after widening : "+ i); OUTPUT:
System.out.println("long value after widening : "+ l);
System.out.println("double value after widening : "+ d); int value after widening : 20
long value after widening : 20
double d2 =20.5; double value after widening : 20.0
byte b2 = (byte)d2; //Narrowing double to byte Narrowing double value to byte : 20
// long ll=d2; //compile time error, must be explicitly casted Narrowing double value to long : 20
long l2= (long)d2; //Narrowing double to long Narrowing double value to float : 20.5
float f2= (float)d2; //Narrowing double to float
System.out.println("Narrowing double value to byte : "+ b2);
System.out.println("Narrowing double value to long : "+ l2);
System.out.println("Narrowing double value to float : "+ f2);
}
}
JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE
Java Operators

Operator in Java is a symbol that is used to perform operations. For example: +, -, *, / etc.

There are many types of operators in Java which are given below:

1. Unary Operator
2. Arithmetic Operator
3. Shift Operator
4. Relational Operator
5. Bitwise Operator
6. Logical Operator
7. Ternary Operator
8. Assignment Operator

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2021 DELIVERED BY : CHINTHAKA DISSANAYAKE


JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE
• Java Unary Operator

The Java unary operators require only one operand. Unary operators are used to perform various
operations i.e.:

• incrementing/decrementing a value by one


• negating an expression
• inverting the value of a boolean

Java Unary Operator Example: ++ and --

public class OperatorExample{ Output:


public static void main(String args[]){
int x=10; 10
System.out.println(x++);//10 (11) 12
System.out.println(++x);//12 12
System.out.println(x--);//12 (11) 10
System.out.println(--x);//10
}}

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


• Java Arithmetic Operators

Java arithmetic operators are used to perform addition, subtraction, multiplication, and division.
They act as basic mathematical operations.

1.public class OperatorExample{ Output:


2.public static void main(String args[]){
3.int a=10; 15
4.int b=5; 5
5.System.out.println(a+b);//15 50
6.System.out.println(a-b);//5 2
7.System.out.println(a*b);//50 0
8.System.out.println(a/b);//2
9.System.out.println(a%b);//0
10.}}

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


40 80 80 240

• Java Shift Operators

a. Java Left Shift Operator

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.

1.public class OperatorExample{ Output:


2.public static void main(String args[]){
3.System.out.println(10<<2);//10*2^2=10*4 40
=40 80
4.System.out.println(10<<3);//10*2^3=10*8 80
=80 240
5.System.out.println(20<<2);//20*2^2=20*4
=80
6.System.out.println(15<<4);//15*2^4=15*1
6=240
7.}}

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


40 80 80 240
252
252
252 • Java Shift Operators

a. Java Right Shift Operator

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.

1.public OperatorExample{ Output:


2.public static void main(String args[]){
3.System.out.println(10>>2);//10/2^2=10/4 2
=2 5
4.System.out.println(20>>2);//20/2^2=20/4 2
=5
5.System.out.println(20>>3);//20/2^3=20/8
=2
6.}}

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


• Java Ternary Operators

Java Ternary operator is used as one line replacement for if-then-else statement and used a lot in
Java programming. It is the only conditional operator which takes three operands.

1.public class OperatorExample{ Output:


2.public static void main(String args[]){
3.int a=2; 2
4.int b=5;
5.int min=(a<b)?a:b;
6.System.out.println(min);
7.}}

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


14 16

• Java Assignment Operator

Java assignment operator is one of the most common operators. It is used to assign the value on
its right to the operand on its left.

1.public class OperatorExample{ Output:


2.public static void main(String args[]){
3.int a=10; 14
4.int b=20; 16
5.a+=4;//a=a+4 (a=10+4)
6.b-=4;//b=b-4 (b=20-4)
7.System.out.println(a);
8.System.out.println(b);
9.}}

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


14 16

• Java Assignment Operator(continue)

1.public class OperatorExample{ Output:


2.public static void main(String[] args){
3.int a=10; 13
4.a+=3;//10+3 9
5.System.out.println(a); 18
6.a-=4;//13-4 9
7.System.out.println(a);
8.a*=2;//9*2
9.System.out.println(a);
10.a/=2;//18/2
11.System.out.println(a);
12.}}

JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE


JAVA DEVELOPING MASTER – EVOTECH EDUCATION 2023 DELIVERED BY : CHINTHAKA DISSANAYAKE

You might also like