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

Lecture 1 - Introduction to Java

This lecture introduces Java, a versatile programming language created in 1995, widely used for mobile, desktop, and web applications. It highlights Java's platform independence, object-oriented structure, and extensive community support, making it a popular choice among developers. The document also outlines the typical phases of Java program development, including editing, compiling, loading, verifying, and executing, as well as key concepts like identifiers, keywords, and data types.

Uploaded by

Asif Hasan Ontu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 1 - Introduction to Java

This lecture introduces Java, a versatile programming language created in 1995, widely used for mobile, desktop, and web applications. It highlights Java's platform independence, object-oriented structure, and extensive community support, making it a popular choice among developers. The document also outlines the typical phases of Java program development, including editing, compiling, loading, verifying, and executing, as well as key concepts like identifiers, keywords, and data types.

Uploaded by

Asif Hasan Ontu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Lecture 1: Introduction to Java

Md. Morshed Ali


Lecturer, Dept. of CSE
Uttara University
morshed.ali@uttarauniversity.edu.bd
What is Java?
• Java is a popular programming language, created in 1995.
• It is owned by Oracle, and more than 3 billion devices run
Java.
• It is used for:
• Mobile applications (specially Android apps)
• Desktop applications
• Web applications
• Web servers and application servers
• Games
• Database connection
• And much, much more!
Why Use Java?
• Java works on different platforms (Windows, Mac, Linux, Raspberry
Pi, etc.)
• It is one of the most popular programming language in the world
• It has a large demand in the current job market
• It is easy to learn and simple to use
• It is open-source and free
• It is secure, fast and powerful
• It has a huge community support (tens of millions of developers)
• Java is an object oriented language which gives a clear structure to
programs and allows code to be reused, lowering development costs
• As Java is close to C++ and C#, it makes it easy for programmers to
switch to Java or vice versa
Features of Java
• Java is platform independent: the same program can
run on any correctly implemented Java system
• Java is object-oriented:
– Structured in terms of classes, which group data with
operations on that data
– Can construct new classes by extending existing ones
• Java designed as
– A core language plus
– A rich collection of commonly available packages
• Java can be embedded in Web pages
Classes & Objects
• The class is the unit of programming
• A Java program is a collection of classes
– Each class definition (usually) in its own .java file
– The file name must match the class name
• A class describes objects (instances)
– Describes their common characteristics: is a blueprint
– Thus all the instances have these same characteristics
• These characteristics are:
– Data fields for each object
– Methods (operations) that do work on the objects
Grouping Classes: The Java API
• API = Application Programming Interface
• Java = small core + extensive collection of packages
• A package consists of some related Java classes:
– Swing: a GUI (graphical user interface) package
– AWT: Application Window Toolkit (more GUI)
– util: utility data structures (important to CS 187!)
• The import statement tells the compiler to make
available classes and methods of another package
• A main method indicates where to begin executing a
class (if it is designed to be run as a program)
A Little Example of import and main
import javax.swing.*;
// all classes from javax.swing
public class HelloWorld { // starts a
class
public static void main (String[] args) {
// starts a main method
// in: array of String; out: none (void)
}
}
• public = can be seen from any package
• static = not “part of” an object
Print Text
• Use the println() method to output values or
print text in Java
• Example:
System.out.println("Hello World!");
System.out.println("I am learning Java.");
System.out.println("It is awesome!");
Print Numbers
• However, unlike text, we don't put numbers
inside double quotes:
System.out.println(3);
System.out.println(358);
System.out.println(50000);
• Output:
3
358
50000
Java Comments
• Comments can be used to explain Java code,
and to make it more readable. It can also be
used to prevent execution when testing
alternative code.
• Single-line Comments
• Single-line comments start with two forward
slashes (//).
• Any text between // and the end of the line is
ignored by Java (will not be executed).
Single-line Comments
• This example uses a single-line comment
before a line of code:
// This is a comment
System.out.println("Hello World");
Java Multi-line Comments

• Multi-line comments start with /* and ends


with */.
• Any text between /* and */ will be ignored by
Java.
• This example uses a multi-line comment (a
comment block) to explain the code:
/* The code below will print the words Hello World
to the screen, and it is amazing */
System.out.println("Hello World");
Typical Java Development Environment
 Java programs normally go through five
phases.
 Edit – create/edit the source code
 Compile – compile the source code
 Load – load the compiled code
 Verify – check against security restrictions
 Execute – execute the compiled code

13
Dept of CSE, University of Dhaka 13
Phase 1: Creating a Program
 Any text editor or Java IDE (Integrated
Development Environment) can be used to
develop Java programs.
 Java source-code file names must end with
the .java extension.
 Some popular Java IDEs are NetBeans, jEdit,
Eclipse, JBuilder, JCreator etc.

14
Dept of CSE, University of Dhaka 14
Phase 2: Compiling a Java Program into Bytecodes
 javac Welcome.java
 Compiles the source file “Welcome.java” (and other files if
necessary), transforms the Java source code into bytecodes and
places the bytecodes in a file named “Welcome.class”.
 It searches the file “Welcome.java” in the current directory and in
the directories listed in the PATH environment variable.
 Bytecodes
 They are not machine language binary code.
 They are independent of any particular microprocessor or
hardware platform.
 They are platform-independent instructions.
 Another entity or interpreter is required to convert the
bytecodes into machine codes that the underlying microprocessor
understands.
 This is the job of the JVM (Java Virtual Machine)

15
Dept of CSE, University of Dhaka 15
JVM (Java Virtual Machine)
 It is a part of the JDK and the foundation of the Java
platform.
 It can be installed separately or with JDK.

 A virtual machine (VM) is a software application that


simulates a computer, but hides the underlying
operating system and hardware from the programs that
interact with the VM.
 One of the main contributors for the slowness of Java
programs compared to compiled machine language
programs (i.e. C, C++, Pascal etc.).

16
Dept of CSE, University of Dhaka 16
JVM (Java Virtual Machine) (contd.)
 It is the JVM that makes Java a portable language.
 The same bytecodes can be executed on any platform
containing a compatible JVM.
 JVM is available for Windows, Unix, Linux and Solaris.
 The JVM is invoked by the “java” command.
 java Welcome
 It searches the class “Welcome” in the current directory and in the
directories listed in the CLASSPATH environment variable and
executes the “main” method of class “Welcome”.
 It issues an error if it cannot find the class “Welcome” or if class
“Welcome” does not contain a method called “main” with proper
signature (more on this will be discussed later)

17
Dept of CSE, University of Dhaka 17
Phase 3: Loading a Program into Memory
 One of the components of the JVM is the class
loader.
 The class loader takes the .class files containing
the programs bytecodes and transfers them to
primary memory (RAM).
 The class loader also loads any of the .class files
provided by Java that our program uses.
 The .class files can be loaded from a disk on our
system or over a network (another PC in our
LAN, or the Internet).

18
Dept of CSE, University of Dhaka 18
Phase 4: Bytecode Verification
 Another component of the JVM is the bytecode
verifier.
 Its job is to ensure that bytecodes are valid and
do not violate Java’s security restrictions.
 This feature helps to prevent Java programs
arriving over the network from damaging our
system.
 Another contributor for making Java programs
slow.

19
Dept of CSE, University of Dhaka 19
Phase 5: Execution
 Now the actual execution of the program begins.

 Bytecodes are converted to machine language suitable for


the underlying OS and hardware.

 Recent JVM performs just-in-time (JIT) compilation to


make program execution faster.

 So, we can say that Java programs actually go through two


compilation phases –
 Source code -> bytecodes
 Bytecodes -> machine language

20
Dept of CSE, University of Dhaka 20
Identifiers, Keyword, Statements

21
Dept of CSE, University of Dhaka 21
Identifiers
 Identifiers are names for variables, classes, methods etc.

 Good ones are compact, but indicate what they stand for
 radius, width, height, length

 Java is case sensitive (so as identifier).

 Rules:
 May contain upper case, lower case letters, numbers, underscore, dollar sign.
 Must not begin with a number.

22
Dept of CSE, University of Dhaka 22
Keywords
 Some words are reserved, and can’t be used as
identifiers

// Authors: J. P. Cohoon and J. W. Davidson


// Purpose: display a quotation in a console window

public class DisplayForecast {

// method main(): application entry point


public static void main(String[] args) {
System.out.print("I think there is a world market for");
System.out.println(" maybe five computers.");
System.out.println(" Thomas Watson, IBM, 1943.");
}
}

23
Dept of CSE, University of Dhaka 23
Capitalization

 Case matters!

 public ≠ Public ≠ PUBLIC


 This is different that FORTRAN and BASIC
 This is the same as C/C++

24
Dept of CSE, University of Dhaka 24
Statements

 A statement in Java is (usually) a single line


 Example: System.out.println (“Hello world!”);

 All statements must end with a semi-colon


(like C)

25
Dept of CSE, University of Dhaka 25
Data types

26
Dept of CSE, University of Dhaka 26
Data Types
 Java is a “strong typed language”
 Each variable has a declared type.

float x; //x is a variable


x = 13.2;

 There are two kinds of data-types in Java:


 Primitive types.
 Classes (will be discussed later).

27
Dept of CSE, University of Dhaka 27
Java Primitive Types

 There are 8 primitive types in Java.


 Integer types:

byte An 8-bit signed integer.

short A 16-bit signed integer.

int A 32-bit signed integer.

long A 64-bit signed integer.

28
Dept of CSE, University of Dhaka 28
Java Primitive Types (Cont.)

 Floating point types:


Float A 32-bit IEEE floating point.

double A 64-bit IEEE floating point.

 Other types:
boolean Either true or false.

char A 16-bit Unicode character.

29
Dept of CSE, University of Dhaka 29
Primitive variable types

 Java has 8 (or so) primitive types:


 float real numbers
 double
 boolean two values: true and false
 char a single character
 byte
 short integer numbers
 int
 long
 Also the “void” type

30
Dept of CSE, University of Dhaka 30
Thank you

Dept of CSE, University of Dhaka 31

You might also like