You are on page 1of 4

Introduction to JAVA

Steps to execute the Java code


- What is Byte Code?
- Java is a platform-independent language
- Execution always starts from the main method
- Object-oriented language
- What are JRE and JDK?

#1 Steps to run the Java Code:- -


We required JVM to run a java code.
- The job of the JVM is to execute the java code that you have created but it does not accept the code directly. -
JVM does not accept the Java code as it only understands the Byte Code. - Java compiler also known as javac
is used to convert java code into byte code. - The extension for the byte code is .class - Thus, the code written
by the programmer is first converted into byte code by the compiler and then that byte code goes to the JVM
for further execution.
Programmer- Java Code- Compiler- Byte Code- JVM

#2 Java is a platform-independent language:- It means you can run Java on any


machine irrespective of any hardware or operating system. The one thing that the machine requires to run a
java code is a JVM.
JVM is platform dependent and it needs to be built for a particular OS.

#3 In Java, execution always starts from the main method:- - JVM starts
execution only with one first file. If you need to run multiple files you need to tell it which one is the first file
through which JVM can start execution.
The first file must contain the main method with its proper signature.
In Java, execution always starts from the main method.
Signature of main method: public static void main(String args[])

#4 Java is an object-oriented language:- - Object-oriented means everything should be


an object. - We always required a class to create an object.

#5 JRE:- - To run the Java application, you need extra libraries and need runtime. Runtime is something
where you run things. - You use extra libraries, and you also need an environment. An additional layer is
present above the OS known as JRE. - JVM is a part of JRE. JRE includes JVM and libraries. JVM - Java
Virutal Machine JRE - Java Runtime Environment - For development purposes, you need to install JDK (Java
Development Kit) which consists of JRE and JVM. - Java is WORA that
is Write Once Run Anywhere provided that JVM and JDK installed.

1
VARIABLES
- Working of Java code
- How is data stored in a database?
- What are variables?
- Syntax of variables

#1 –
 As a Java developer you write a Java code that will be having .java extension and that gets compiled
by javac.
 When you compile that code what you get in output is a byte code that has .class file.
 Whenever you run .class file in the JVM, it means that it's a part of JRE that has JVM as well as
libraries and a lot of classes, and JRE is installed on the OS.
 So, you need a hardware or machine having OS and then OS will have JRE and it has JVM. As a
developer, one more tool is required known as JDK. When you will install the new JDK, you will get
updated JRE and JVM with it.
 We are using JDK version 17 in this course because of its long-term support. - We develop software to
solve real-world problems using the virtual world. e.g., Amazon

#2 –
1. Whenever we build a project or an application what we are doing is we are processing the data.
2. Data may come from the user and then we do processing on it. A database is used to store the data and
processing done on it.
3. Database is permanent or persistent storage which means data will remain there if the machine gets
shut down.

#3 Variables:- -
 Variable is used to store the data temporarily while processing.
 Variable is a type of box in which you store data and a type is attached to it such as type can be
number, text, image, etc. Thus, a box is known as a variable and a name and value are present with it.
- A variable is a container that holds the value while the Java program is executed.
 Java is a strongly-typed language as it demands the declaration of every variable with a data type.
 Syntax of creating a variable:
 datatype variable name = value; int num1= 3;
 Here, (=) is the assignment operator that takes the right-hand side value and assigns it to the left-hand
side.
 println is used to go or print on the new line. - In Java, every statement ends with a semicolon except
blocks.

2
DATATYPES
In this lecture we are discussing
1) datatypes for given variable
a) Types of data-types?
b) primitive data types
c)why do we need different type of integer type?
d)how to create variable of different data types?

#1
a) Datatypes are categories into primitive and non-primitive datatype. But in this lecture, we are take all
concept upon primitive data type.

b) There are 8 primitive datatype :


integer ---byte, short, int, long
float --- float , double
character ---char
boolean --- boolean

c) why do we need different type of integer ?


there are range for each type. for int --4 bytes , long --8 bytes, short --2bytes ,byte --1byte

d) How to create variable of different data type?


 int a=10; byte b=4
 dataType variableName = value;

integer variable declaration:


 int a=5;
 long b=600l; // e.g 578l, 100000l represent long integer type
 byte c=127;
 short s=128;

float vs double:
 double have more precision than float
 by default java support double.
 declare variable of float and double type
 double a=5.6;
 but float b=5.6; //give error --correct way float b=5.6f;

character representation:
 char ch='a'; //using single quotes on character we can declare char type of variable
boolean representation of variable:

3
 boolean result=true;
 there are only two value of boolean either true or false.
 In other language like c and c++ -- 0 is treated as false and non zero number treated as true but it is
not in java.
 {byte, short, int, long, float, double, char, Boolean } since java is statically type or say strongly type
language therefore we need to mention datatype for each variable for declaring variable.
 e.g int i=5; float f=2.5f; In java integer are by default for integer int type; for real number by default
double

TYPE CONVERSION IN JAVA


in this lecture we are discussing:
1)What is type conversion or type casting ?
2)Different ways to casting?
a)implicit type casting or automatic type casting
b)explicit type casting
3)What is effect of implicit and explicit type casting?
a) Narrowing conversion
4)Type promotion when we do operation

#1 What is type conversion or type casting ? –

type conversion or type casting is the process of converting a value from one data type to another data type. e.g
int num=5; long l=num; #2 Different ways to casting a) Implicit type casting :- It is way to in which compiler
automatically convert smaller size data type in larger. e.g int num=4; long l=num; //now num value converted
to long b) Explicit type casting :- manually when programmer cast one data type into other is known as explicit
type casting. e.g float fl=4.5f; int num=fl; -- num value become 4; syntax for conversion: type1 x=value;
//higher size type2 y=(datatype of type2)x; #3 What is effect of type casting ? -- one effect is narrowing
conversion i.e Narrowing conversions can be done from a larger data type to a smaller data type, but they can
result in loss of precision or data. e.g float fl=5.6f; int num=fl; loss of 0.6 precision now value of num is 5.

You might also like