Topic 2 - Java Programming Basics (Part 1)
Topic 2 - Java Programming Basics (Part 1)
▪ It is easier to learn.
import statements
Class declarations
IMPORT STATEMENTS
Syntax:
[<package_name>].<class_name>
Packages
A group of pre-defined classes.
Can include sub-packages forming a hierarchy of packages.
Example:
java.util.Scanner
Classes
A program is defined by using one or more classes
Methods
A collection of statements that perform sequence of operations to display message to the
standard output device. Example, System.out.println();
Modifiers
Specify the properties of the data, methods and classes and how they can be used.
Examples are public, private and static.
Statement
Represent an action or a sequence of actions
Every statement ends with a semicolon (;)
Blocks
The braces in the program form a block that groups the component of the program. Every
class begin with opening braces ({)and ends with closing braces (})
EXAMPLE
} //end of class
WHY USE STANDARD CLASSES
Don’t reinvent the wheel. When there are existing libraries that satisfy our needs, just
use them.
Learning how to use standard Java classes is the first step toward mastering OOP.
Before we can learn how to define our own classes, we need to learn how to use
existing classes.
Example of standard classes:
JOptionPane
String
Date
JOPTIONPANE
To represent integral values use type int. If a larger range of values is needed, use type
long.
On some computers, long will take longer to execute, so care may be needed if a lot of
arithmetic is being performed.
To represent non-integral values, use type double. Type float has similar properties
but less precision and a smaller range.
If speed of execution is very important, the float type may offer advantages on some
computers.
These are all what we call primitive types in Java. There are other types available.
RELATIONAL OPERATOR
We can change the value of a variable. If we want the value to remain the same, we use a
constant.
Numerical data are called primitive data types while Objects are called
reference data types
Primitive type variables directly store data into their memory space.
Reference variables store the address of the object containing the data.
✓ Object variables are used to access an object.
✓ Contains a pointer to a desired object.
PRIMITIVE DATA DECLARATION AND ASSIGNMENTS
It shows how two memory locations (variables) with name firstNunmer and
secondNumber are declared and values are assigned to them
ASSIGNING NUMERICAL DATA
ASSIGNING OBJECTS
WRAPPER CLASS
• Java provides routine for converting String to a primitive data types or vice versa
• It is called wrapper classes because the classes constructed by wrapping a class structure
around the primitive data types
int age;
String inputStr;
inputStr = “256”
age = Integer.parseInt(inputStr);
WRAPPER CLASSES
OBJECT DATA TYPE – CLASS STRING
str1.length() -> 5
str2.length() -> 4
str3.length() -> 0
str4.length() -> 1
OBJECT DATA TYPE – INDEXOF
Assume str and substr are String objects and properly initialized.
str.indexOf(substr) will return the first position substr occurs in str
If str is “programming” and substr is “gram”, then str.indexOf(substr)
will return 3 because the position of the first character of substr in str is 3.
If substr does not occur in str, then -1 is returned.
The search is case-sensitive.
OBJECT DATA TYPE – INDEXOF
String str;
str = “I Love Java and Java loves me.”;
3 7 21
If there is more than one occurrence of the same substring, the index
position of the first character of the first matching substring is
returned.
OBJECT DATA TYPE – CONCATENATION
• Assume str1 and str2 are String objects and properly initialized.
• str1 + str2 will return a new string that is a concatenation of the
two strings.
• If str1 is “pro” and str2 is “gram”, then str1 + str2 will return
“program”.
• Notice that this is an operator and not a method of the String
class.
• The strings str1 and str2 remains the same.
OBJECT DATA TYPE – CONCATENATION