Lecture-3
Java Basic and Program
With Java
Discussion Topics
• Introduction to Java
• Keywords in Java
• Data Types in Java
• Java Identifiers
• Java Variables
• Java Modifiers
6/19/2020 sazzad@diucse
Introduction to Java
6/19/2020 sazzad@diucse
Introduction to Java
• Java is a high-level programming language originally developed by Sun
Microsystems and released in 1995.
• Java runs on a variety of platforms, such as Windows, Mac OS, and
the various versions of UNIX.
6/19/2020 sazzad@diucse
Features of Java
• Object Oriented: In Java, everything is an Object. Java can be easily extended since it is
based on the Object model.
• Platform independent: Unlike many other programming languages including C and C++,
when Java is compiled, it is not compiled into platform specific machine, rather into platform
independent byte code. This byte code is distributed over the web and interpreted by virtual
Machine (JVM) on whichever platform it is being run.
• Simple: Java is designed to be easy to learn. If you understand the basic concept of OOP
Java would be easy to master.
• Secure: All the code is converted in byte code after compilation, which is not readable by a
human. and java does not use an explicit pointer and run the programs inside a package.
• Portable: Being architectural-neutral and having no implementation dependent aspects of
the specification makes Java portable. Compiler in Java is written in ANSI C with a clean
portability boundary.
6/19/2020 sazzad@diucse
Some of the Other Features of Java
• Robust
• Multithreaded
• Interpreted
• High Performance
• Distributed
• Dynamic
6/19/2020 sazzad@diucse
Popular Java Editors
To write your Java programs, you will need a text editor and you can consider
one of the following:
• Notepad: On Windows machine you can use any simple text editor like
Notepad, TextPad.
• Netbeans: is a Java IDE that is open-source and free which can be
downloaded from [Link]
• Eclipse: is also a Java IDE developed by the eclipse open-source community
and can be downloaded from [Link]
6/19/2020 sazzad@diucse
Simple Java Program
public class MyFirstJavaProgram {
public static void main(String []args) {
[Link]("Hello World");
}
}
6/19/2020 sazzad@diucse
Keywords in java
6/19/2020 sazzad@diucse
Java Keywords
6/19/2020 sazzad@diucse
Data Types in java
6/19/2020 sazzad@diucse
Data Types
6/19/2020 sazzad@diucse
Primitive Data Types in Java
6/19/2020 sazzad@diucse
Identifiers in java
6/19/2020 sazzad@diucse
Java Identifiers
Names used for classes, variables and methods are called identifiers. All identifiers
must follow the following rules:
An identifier is a sequence of characters that consists of letter (A to Z or a to z), digits(0 to
9), currency character ($) or an underscore (_).
All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an
underscore (_).It can not start with a digit (0-9).
A keyword cannot be used as an identifier.
Most importantly identifiers are case sensitive.
area, Area, AREA are all different identifiers
Examples of legal identifiers: age, $salary, _value, __1_value
Examples of illegal identifiers: 123abc, -salary
6/19/2020 sazzad@diucse
Exercise
• Find out the legal Identifiers
$2 2A area d+4 Max-Number
Min_number aaa sales_tax _circleArea a2b
Box100width /$directory ab1234$$ ComputeArea
6/19/2020 sazzad@diucse
Variables in java
6/19/2020 sazzad@diucse
Variables in java
Variables
Class/Static
Local Variables Instance Variables
Variables
6/19/2020 sazzad@diucse
Local variables
• Local variables are declared inside methods, class A // It’s a class
{
constructors, or blocks. int I =50; // instance variable
static int S =100; // static variable
• Local variables are visible only within the declared void method() // It’s a Method
{
method, constructor or block. int L =90; // local variable
}
• Access modifiers cannot be used for local variables. }
6/19/2020 sazzad@diucse
Instance variables
• Instance variables are declared in a class, but class A // It’s a class
outside a method, constructor or any block. {
int I =50; // instance variable
• Access modifiers can be assign for instance static int S =100; // static variable
variables.
void method() // It’s a Method
• The instance variables are visible for all methods, {
int L =90; // local variable
constructors and block in the class. }
}
6/19/2020 sazzad@diucse
Class/static variables
• Class variables also known as static variables are class A // It’s a class
declared with the static keyword in a class, but {
outside a method, constructor or a block. int I =50; // instance variable
static int S =100; // static variable
• Static variables are rarely used other than being
void method() // It’s a Method
declared as constants. {
int L =90; // local variable
• Visibility is similar to instance variables. }
}
• However, most static variables are declared public
since they must be available for users of the class.
6/19/2020 sazzad@diucse
Example
public class MyClass { public static void main(String[] args) {
public int I = 50; //Instant Variable MyClass obj = new MyClass();
static int S = 100 ; //Static Variable obj.method1();
obj.method2();
public void method1(){
}
int L = 90; //Local Variable
}
[Link](L);
[Link](I); //Can use the Instant Variable
Output:
[Link](S); //Can use the Static Variable
90
}
public void method2(){ 50
[Link](L); //Error!! Can not use the Local Variable 100
} Exception in thread
6/19/2020 sazzad@diucse
End of First Part
6/19/2020 sazzad@diucse
Modifiers in java
6/19/2020 sazzad@diucse
Modifiers
Modifiers are keywords that you add to those definitions to change their
meanings.
There are two types of modifiers in java:
• Access modifiers
• Non-access modifiers
6/19/2020 sazzad@diucse
Access Modifiers
6/19/2020 sazzad@diucse
Access Modifiers in java
• 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
6/19/2020 sazzad@diucse
Private Access Modifier
• The private access modifier is accessible only within the class.
Simple example of private access
modifier
In this example, we have
created two classes A and Simple. A
class contains private data member
and private method. We are
accessing these private members
from outside the class, so there is
compile time error.
6/19/2020 sazzad@diucse
2. Default Access Modifier
• If you don't use any modifier, it is treated as default by default.
• The default modifier is accessible only within package.
6/19/2020 sazzad@diucse
Default Access Modifier
If we don't use any modifier, it is treated as default by default. The default
modifier is accessible only within package.
Example:
In this example, we have created two
packages pack and mypack. We are
accessing the A class from outside its
package, since A class is not public, so it
cannot be accessed from outside the
package.
In the above example, the scope of class A
and its method msg() is default so it cannot
be accessed from outside the package.
6/19/2020 sazzad@diucse
Protected Access Modifier
• The protected access modifier is accessible within package and
outside the package but through inheritance only.
• The protected access modifier can be applied on the data member,
method and constructor. It can't be applied on the class.
6/19/2020 sazzad@diucse
Example:
In this example, we have created
the two packages pack and
mypack. The A class of pack
package is public, so can be
accessed from outside the package.
But msg method of this package is
declared as protected, so it can be
accessed from outside the class
only through inheritance.
6/19/2020 sazzad@diucse
Public Access Modifier
• The public access modifier is
accessible everywhere.
• It has the widest scope among all
other modifiers.
• Example of public access
modifier
6/19/2020 sazzad@diucse
Understanding all Java Access Modifier
6/19/2020 sazzad@diucse
Non-Access Modifiers
6/19/2020 sazzad@diucse
Non-Access Modifiers
• static modifier
for creating class methods and variables
• final modifier
for finalizing the implementations of classes, methods, and variables.
• abstract modifiers
for creating abstract classes and methods.
• synchronized and volatile modifiers
which are used for threads.
6/19/2020 sazzad@diucse
Thank You
6/19/2020 sazzad@diucse