Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

UNIVERSITY OF IRINGA

FACULTY OF SCIENCE AND EDUCATION


DEPARTMENT OF INFORMATION TECHNOLOGY

LECTURE TWO
INTRODUCTION TO JAVA

Reg, No. Course Year


DIT 206 Introduction to OOP 2018/ 2019

Ass. Lecturer: Thobius Joseph (M.Sc. In Telecom Eng.)


Date: Dec, 2018

1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724


CONTENTS

1. Naming convention

2. Class

3.Program Structure

1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724


Required Knowledge

Basic Knowledge of C Programming and C++ will


help you to understand Java Programming
quickly, and If you don't know programming and
you are studying Java, so it's quite complicated.

1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724


Class Structure
 A class is a blueprint from which individual objects are created. It consist of
access modifiers + Class keyword + Name of Class + Braces { inside here is
where we put code}
 Assume class as folder and objects as files in folder, each student files in that
folder will be structured the same, student identification, courses …

Example: public class Dog { private class Car {


String breed; int currentSpeed;
int age; String name;
String color; public void accelerate() {
void barking() { } }
void hungry() { }
void sleeping() { } public void park() {
} }

public void printCurrentSpeed() {


}}

 Also the methods or functions in class consist of access modifiers + + Name of


method + Braces { inside here is where we put code}
 Other statements must end with semicolon ;

1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724


Class Structure
 A Java program is a collection of classes.

class Computer {
Computer() {
}
void computer_method() {
}
public static void main(String[] args) {
} /* main method in the class, its syntax wont change in any class and can
appear in many classes of one single source code, you just call a class with a main
you want it to be executed */
}

class Laptop {
Laptop() {}
void laptop_method() { }
}

1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724


Program Structure
 Most of time we write each class in a separate file and the name of
the file is the name of the class contained in the file, with the
extension .java. Thus, the class stack would be stored in a file called
stack.java
NB:As long as you don't have a public class in your source file, you can
name your source file to any name and can compile. But, if you have a
public class in your source file, that file should have the name same as
your class name. Otherwise, compiler will throw an error.
 The public class name must match the file name. Inner, non public,
class names may differ.
 You should compile the program with file name and you should run
the program with the class name in which the main method exist.
main methods may be multiple in different classes so you should
run it with the class name in which the main method you want to
run

1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724


Program Structure
●Package Statement
●Documentation Section
●Import Statements
●Interface Statement
●Class Definition
●Main Method Class
●Main Method Definition

1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724


Program Structure
● We will use HelloWorld Java program to understand structure and features of
the class.
1.“package sct”:
 It is package declaration statement. The package statement defines a namespace
in which classes are stored. The package is used to organize the classes based on
functionality. If you omit the package statement, the class names are put into the
default package, which has no name. Package statement cannot appear anywhere
in the program. It must be the first line of your program or you can omit it.
 You can create a package with any name. A package is a group of classes that are
defined by a name. That is, if you want to declare many classes within one
element, then you can declare it within a package. It is an optional part of the
program, i.e., if you do not want to declare any package, then there will be no
problem with it, and you will not get any errors. Here, the package is a keyword
that tells the compiler that package has been created. It is declared as:

package package_name; (show exapmle using notepad++)


 There can be only one package statement in each source file, and it applies to all
types(classes, interfaces, enumerations, and annotation types) in the file.

1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724


Program Structure
● a package called graphics, for 3 source files, like this:
//in the Draggable.java file
package graphics;
public interface Draggable {
...
}

//in the Graphic.java file


package graphics;
public abstract class Graphic {
...
}

//in the Circle.java file


package graphics;
public class Circle extends Graphic
implements Draggable {
...
}

//in the Rectangle.java file


package graphics;
public class Rectangle extends Graphic
implements Draggable {
...
}
If you do not use a package statement, your type ends up in an unnamed package. Generally speaking, an
unnamed package is only for small or temporary applications or when you are just beginning the development
process. Otherwise, classes and interfaces belong in named packages.

1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724


Program Structure
● We will use HelloWorld Java program to understand structure and features
of the class.
2.“public class HelloWorld”:
 This line has various aspects of java programming.
 a. public: This is access modifier keyword which tells compiler access to class. Various values of access
modifiers can be public, protected, private or default (no value).
 b. class: This keyword used to declare a class. Name of class (HelloWorld) followed by this keyword.

Access Modifiers in java


There are two types of modifiers in java: access modifiers and non-access modifiers. The access modifiers in
java specifies accessibility (scope) of a data member, method, constructor or class. There are 4 types of java
access modifiers:
1. private
2. default
3. protected
4. Public
There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc.

1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724


Program Structure
● We will use HelloWorld Java program to understand structure and features
of the class.
3. Comments section (Documentation):
You can write a comment in this section. Comments are beneficial for the programmer
because they help them understand the code. These are optional, but we suggest you use
them because they are useful to understand the operation of the program, so you must
write comments within the program.

We can write comments in java in two ways.

a. Line comments: It starts with two forward slashes (//) and continues to the end of the
current line. Line comments do not require an ending symbol.

b. Block comments start with a forward slash and an asterisk (/*) and end with an asterisk
and a forward slash (*/).Block comments can also extend across as many lines as needed.

1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724


Program Structure
● We will use HelloWorld Java program to understand structure and features
of the class.
4. “public static void main (String * + args)”:
Its method (Function) named main with string array as an argument.
Every Java stand-alone program requires the main method as the starting point of the
program. This is an essential part of a Java program. There may be many classes in a Java
program. Methods contain data type declaration and executable statements.

It has the following:


a. public: Access Modifier

b. static: static is a reserved keyword which means that a method is accessible and usable
even though no objects of the class exist.

c. void: This keyword declares nothing would be returned from the method. The method
can return any primitive or object.

d. Method content inside curly braces. { }

1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724


Program Structure
● We will use HelloWorld Java program to understand structure and features
of the class.
5. System.out.println("Hello World from Java") :

a. System: It is the name of Java utility class.

b. out:It is an object which belongs to System class.

c. println: It is utility method name which is used to send any String to the console.

d. “Hello World from Java”: It is String literal set as argument to println method

6. Import statements
This line indicates that if you want to use a class of another package, then you can do this
by importing it directly into your program.
Example:
import calc.add;

1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724


Again..
 A Java program is a collection of classes. Most of time we write
each class in a separate file and the name of the file is the name of
the class contained in the file, with the extension .java. Thus, the
class stack would be stored in a file called stack.java
NB:As long as you don't have a public class in your source file, you can
name your source file to any name and can compile. But, if you have a
public class in your source file, that file should have the name same as
your class name. Otherwise, compiler will throw an error.
 The public class name must match the file name. Inner, non public,
class names may differ.
 You should compile the program with file name and you should run
the program with the class name in which the main method exist.
main methods may be multiple in different classes so you should
run it with the class name in which the main method you want to
run

1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724


Program Structure
Example:
Filename: TestFileName.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello,World\n");
}
}
Compiling: javac TestFileName.java
Error:
TestFileName.java:1: class HelloWorld is public,
should be declared in a file named HelloWorld.java
public class HelloWorld
^
1 error

1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724


Program Structure

Example:
//Name of this file will be "Hello.java"

public class Hello


{
/* Author: www.w3schools.in
Date: 2018-04-28
Description:
Writes the words "Hello Java" on the screen */
public static void main(String[] args)
{
System.out.println("Hello Java");
}
}

Program Output:
Hello Java

1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724


Program Structure
public class Hello
●This creates a class called Hello.
●All class names must start with a capital letter.
●The public word means that it is accessible from any other classes.

/* Comments */
The compiler ignores comment block. Comment can be used anywhere in the program to
add info about program or code block, which will be helpful for developers to understand
the existing code in the future easily.
Braces
Two curly brackets {...} are used to group all the commands, so it is known that the
commands belong to that class or method.
public static void main
●When the main method is declared public, it means that it can also be used by code
outside of its class, due to which the main method is declared public.
●The word static used when we want to access a method without creating its object, as we
call the main method, before creating any class objects.
●The word void indicates that a method does not return a value. main() is declared as void
because it does not return a value.
●main is a method; this is a starting point of a Java program. You will notice that the main
method code has been moved to some spaces left. It is called indentation which used to
make a program easier to read and understand.
1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724
Program Structure
String[] args
It is an array where each element of it is a string, which has been named as "args". If your
Java program is run through the console, you can pass input parameter, and main() method
takes it as input.

System.out.println();
This statement is used to print text on the screen as output, where system is a predefined
class, and out is an object of the PrintWriter class defined in the system. The method println
prints the text on the screen with a new line. You can also use print() method instead of
println() method. All Java statement ends with a semicolon.

NB:
●You have to keep in mind that, Java code is case sensitive.
●To write Java program, you must have to define class first.
●The name of the class in Java (which holds the main method) is the name of the Java
program, and the same name will be given in the filename. As mentioned above in the
sample program; The name of the class is "Hello" in which the main method is, then this file
will be named "Hello.Java".

1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724


Calling main method into other class
Create a file Sample.java
class A
{
public static void main(String args[])
{
String str[] = {""};
System.out.println("hi");
B.main(str);
}
}
class B
{
public static void main(String args[])
{
System.out.println("hello");
}
}
now you compile it as javac Sample.java and run as java A then output will be
hi
hello
or you run as java B then output will be
hello
Notice that none of the classes are marked public therefore giving them default access. Files without
any public classes have no file naming restrictions.
1/16/2020 Thobius Joseph(Msc in Eng.), 0783758724

You might also like