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

Module1.2_Introduction_Java

The document provides an introduction to Java, detailing its development history, data types, variables, and programming concepts such as static vs non-static methods. It covers essential programming constructs including operators, control statements, type conversion, and arrays. Additionally, it discusses the scope and lifetime of variables, as well as dynamic initialization in Java.

Uploaded by

nabinkoirala53
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)
4 views

Module1.2_Introduction_Java

The document provides an introduction to Java, detailing its development history, data types, variables, and programming concepts such as static vs non-static methods. It covers essential programming constructs including operators, control statements, type conversion, and arrays. Additionally, it discusses the scope and lifetime of variables, as well as dynamic initialization in Java.

Uploaded by

nabinkoirala53
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/ 35

Introduction to Java

Amar Jukuntla,
Assistant Professor, ACSE,
VFSTR University
Index
• Introduction
• Data Types
• Variables
• Dynamic Initialization
• Scope and lifetime of variables
• Operators and Control Statements
• Type conversion and casting
• Arrays
• References

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Introduction
• Java developed by James Gosling , Patrick Naughton, Chris Warth,
Ed Frank, and Mike Sheridan at Sun Microsystems in 1991.
• It took 18 months to develop the first working version and this language was
initially called “OAK”.
• Name changed to JAVA and First public release in 1995.

• Machine-independent and Object Oriented Programming language.


• Java source code is turned into simple binary instructions, much like ordinary
microprocessor machine code.
• Compiled Java byte-code is executed by a Java runtime interpreter.

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Data Types
• Java is Strongly Typed Language.
• First, every variable has a type, every expression has a type, and every type is
strictly defined.
• Second, all assignments, whether explicit or via parameter passing in method
calls, are checked for type compatibility.
• There are no automatic conversions of conflicting types as in some
languages.
• The Java compiler checks all expressions and parameters to ensure that
the types are compatible. Any type mismatches are errors that must be
corrected before the compiler will finish compiling the class.

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Data Types
• Primitive Data Types
• byte
• short
Integers
• int
• long
• char Characters like letters,
symbols

• float Floating
Points
• double
• boolean True/False

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Amar Jukuntla | Department of Advanced Computer Science and Engineering
Variables
• Examples of variable declarations of various types.
Note that some include an initialization.
• int a,b,c;
• int d=3, e, f=5;
• byte z=22;
• double pi=3.14159;
• char x=‘x’;

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Java Programming Style

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Java Program

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Java Compilation

JIT Compiler: Compiles bytecode into native code for improved performance.

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Execution of Java Programs: Static vs Non-Static
Context
• In Java, the execution of a program depends on whether the main
method and other methods in the program are declared with the static
keyword or not.
With static
Definition of static: The static keyword in Java indicates that a method or variable
belongs to the class rather than any instance of the class. It can be accessed without
creating an object of the class.
Execution Flow:
•The Java Virtual Machine (JVM) starts execution from the main method.
•The main method must always be declared as public static void main(String[]
args) to be recognized by the JVM as the entry point.
•Other static methods and variables can be accessed directly within the main
method without creating an instance of the class.
Amar Jukuntla | Department of Advanced Computer Science and Engineering
Example
public class StaticExample {
static void displayMessage() {
System.out.println("Static method called!");
}
public static void main(String[] args) {
displayMessage(); // Direct call
}
} Output:
Static method called!

Advantages of static:
•No need to create an instance of the class.
•Useful for utility or helper methods.

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Without static
Execution Flow:
• The main method is still required to be static for the program to
run, as the JVM invokes it without instantiating any object.
• To call non-static methods or access non-static variables, an object of
the class must be created in the main method.

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Example
public class NonStaticExample {
void displayMessage() {
System.out.println("Non-static method called!");
}
public static void main(String[] args) {
NonStaticExample example = new NonStaticExample(); // Create an object
example.displayMessage(); // Call the method using the object
}
}
Output:
Non-static method called!

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Why main cannot be non-static:
•If the main method were not static, the JVM would need
to create an instance of the class to invoke it.
• This creates a circular dependency, as there would be no way to
instantiate the class without first calling main.

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Dynamic Initialization
• Java allows its programmers to initialize a variable at run
time also. Initializing a variable at run time is called dynamic
initialization.
• For example, here is a short program that computes the length
of the hypotenuse of a right triangle given the lengths of its
two opposing sides:

𝑐= 𝑎 2 + 𝑏 2

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Scope and lifetime of variables
• Java allows variables to be declared within
any block.
• A block begins with an opening curly brace { and ends
with closing curly brace }. That block defines scope.
• The scope determines what objects are visible to other parts
of our program and life time of those objects.
• Types of scopes
• Class Scope
• Method Scope

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Example

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Operators and Control Statements
•Operators
• Arithmetic Operator
• Assignment Operator
• Relational Operator
• Logical Operator
• Bitwise Operator
• Unary Operator
• Shift Operator
• Ternary Operator
Amar Jukuntla | Department of Advanced Computer Science and Engineering
Amar Jukuntla | Department of Advanced Computer Science and Engineering
CONTROL
STATEMENTS

Conditional Iteration Jump


Statements Statements Statements

If else Nested If Switch case Break Continue

While Do While For


switch

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Amar Jukuntla | Department of Advanced Computer Science and Engineering
break

Amar Jukuntla | Department of Advanced Computer Science and Engineering


continue

Amar Jukuntla | Department of Advanced Computer Science and Engineering


For Loop
int a=5;

Amar Jukuntla | Department of Advanced Computer Science and Engineering


While Loop

Amar Jukuntla | Department of Advanced Computer Science and Engineering


do-while loop

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Type conversion and casting
• The process of converting the value of one data type (int,
float, double, etc.) into another data type is known as Type
Casting.
• Type Casting is the temporary conversion of a variable from its
original data type to some other data type, like
being cast for a part in a play or movie.

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Type conversion and casting

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Example
int x = 7;
//automatically converts the integer type into long type
long y = x;
//automatically converts the long type into float type
Widening
float z = y;

double d = 166.66;
//converting double data type into long data type
Narrow long l = (long)d;
//converting long data type into int data type
int i = (int)l;
Amar Jukuntla | Department of Advanced Computer Science and Engineering
Arrays

Amar Jukuntla | Department of Advanced Computer Science and Engineering


Arrays

int[] numbers = {1, 4, 5, 8, 7};

Amar Jukuntla | Department of Advanced Computer Science and Engineering


References
• https://www3.ntu.edu.sg/home/ehchua/programming/java/J1a_Introdu
ction.html
• https://dotnettutorials.net/lesson/operators-in-java/
• https://www.sitesbay.com/java/java-control-flow-statements-in-java
• https://web.stanford.edu/class/archive/cs/cs106a/cs106a.1154/lectures/
05/Slides05.pdf

Amar Jukuntla | Department of Advanced Computer Science and Engineering


THANK
YOU

You might also like