2. Introduction to Java

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Introduction

to Java
Java
• Java is a high-level (object-
oriented) programming language
originally developed by Sun
Microsystems and released in
1995.
• Java is now maintained and
distributed by Oracle Corporation
since 2010.
• Java derives most of its syntax
© K.S.from
Mbise C and C++
Introductionprogramming
to Java Slide 2
Java cont…
• Java systems contain:
• Environment – hardware or
software environment in which a
program runs.
• Language – the Java language
itself
• APIs – Java class libraries
• Java Virtual Machine (JVM)
© K.S. Mbise Introduction to Java Slide 3
Advantages of Java
• Easy to learn
• Pure object-oriented language
• Extensive documentation
• Faster application development
because of code reuse
• Platform independence (Write
once, run anywhere)
• Automatic garbage collection
© K.S. Mbise Introduction to Java Slide 4
Writing Java application
• In Java, all codes must reside
inside a class.
• The name of a source file is very
important.
• By convention, the name of that
class should match the name of
the file that holds the program.

© K.S. Mbise Introduction to Java Slide 5


Writing Java application
cont…
• However, it is the name of the
public class that must match the
name of the Java source code file.
• A source code file uses the .java
filename extension.
• Java is case-sensitive.

© K.S. Mbise Introduction to Java Slide 6


Writing Java application
cont…
/* Simple Java application that displays
"Hello World!" */
public class Hello {
/*main method begins execution of
Java application*/
public static void main(String[] args)
{
System.out.println("Hello
World!");
}
© K.S. Mbise Introduction to Java Slide 7
Writing Java application
cont…
• class – a keyword that precedes
the name of class
• public – means that the class
Hello and method main are
available to other classes and
objects
• static – means that main() is a
class method.
© K.S. Mbise Introduction to Java Slide 8
Writing Java application
cont…
• void – means that main() method
does not return a value
• main(String[] args) – a main
method that takes one parameter,
which is an array of strings.
• This argument is used for program
arguments.

© K.S. Mbise Introduction to Java Slide 9


Writing Java application
cont…
• println() is a built-in method used
for handling output
• System is a predefined class that
provides access to the system
• out is the output stream that is
connected to the console (monitor
or display screen)

© K.S. Mbise Introduction to Java Slide 10


Compiling/Running Java
application
• Assuming that you have typed
your Java program in IntelliJ IDEA
Community Edition IDE, follow
these steps:
• On menu bar, select Run  Run
FileName.

© K.S. Mbise Introduction to Java Slide 11


Compiling Java application
cont…
• Alternatively, use the following
steps:
• C:>\javac Hello.java (use
terminal for Linux) .
• The javac compiler creates a file
called Hello.class that contains
the bytecode version of the
program.
• To actually run program, you
must use Introduction
© K.S. Mbise the Java to Javainterpreter,
Slide 12
Compiling Java application
cont…
• C:>\java Hello
• The bytecode file name is written
without filename extension
• When Java source code is
compiled, each individual class is
put into its own output file named
after the class and using
the .class extension.

© K.S. Mbise Introduction to Java Slide 13


Main class and main
method
• A class that contains main
method is called main class.
• Each Java program must have
one class that has a main
method.
• The main method is the entry
point, which is used to start the
program by creating some
© K.S. objects
Mbise andIntroduction
invoking to Java their Slide 14
Comments in Java
• Comments make the code
readable
• Give comments wherever
necessary
• // – this is a single line (end-of-
line) of comments
• /* – this is a block of comments
(multiple-line) */
© K.S. Mbise Introduction to Java Slide 15
Syntax of Java class
• The syntax of a simple class
modifier class ClassName {
modifier data-type field1;
modifier data-type field2;
...
modifier data-type fieldN;
modifier return-type
methodName1(parameters){
//statements
}
© K.S. Mbise Introduction to Java Slide 16
Syntax of Java class cont…
modifier return-type
methodName2(parameters){
//statements
}
...
modifier return-type
methodN(parameters) {
//statements
}
© K.S.}Mbise Introduction to Java Slide 17
Data types
• Data types in Java are divided into
two categories - primitive types and
reference types (sometimes called
nonprimitive types).
• The primitive types are boolean,
byte, char, short, int, long, float and
double.
• All nonprimitive types are reference
types, so classes, which specify the
© K.S. Mbise Introduction to Java Slide 18
types of objects, are reference
Data types cont…
• Integers
• byte (8 bit)
• short (16 bit)
• int (32 bit)
• Floating-point numbers
• float (32 bit)
• double (64 bit)
• Characters (char, 8 bit)
• Booleans (boolean, true or false)
© K.S. Mbise Introduction to Java Slide 19
Variable definition
• modifier(s) DataType
VariableName = Value;
• Modifier(s) can be either access,
or non-access modifier.
• private int x = 5;
• boolean b;
• final int I = 8; //constant variable

© K.S. Mbise Introduction to Java Slide 20


Java conventions
• Class names start with an
uppercase
– MyClass
• Variable names start with a
lowercase
– myVariable
• Method names start with a
lowercase
© K.S. Mbise Introduction to Java Slide 21
– myMethod
Java conventions cont…
• Final variables are always
capitalized (and initialized)
• E.g., final double PI = 3.1415;

• Always give meaningful names to


your variables, methods, and
classes.
• Use indentation where necessary

© K.S. Mbise Introduction to Java Slide 22


Thank you for listening!

© K.S. Mbise Introduction to Java Slide 23

You might also like