You are on page 1of 30

Object Oriented Programming

(Java)
Module 2
Fundamentals of Java Language
• Learn the basic syntax and language rules of Java.

• Understand the difference between identifiers and keywords


• Understand the use of different comments used in Java
• Understand the different uses of Java data types

• Understand the use of System.out.print in displaying an output


• Understand the use of Scanner and BufferedReader in getting input
from user
Basic Structure of a Java Program

2.1

Fundamentals of Java Language


BASIC STRUCTURE OF A JAVA CLASS

class HelloWorld{
public static void main(String args[ ]){
System.out.println(“Hello World”);
}
}

class HelloWorld – defines a class, a


template for an object of derived type
HelloWorld

Fundamentals of Java Language


BASIC STRUCTURE OF A JAVA CLASS

class HelloWorld{
public static void main(String args[ ]){
System.out.println(“Hello World”);
}
}

public – access specifier/modifier, the main


method is declared public so that it is
accessible as part of the public interface of
the program.

Fundamentals of Java Language


BASIC STRUCTURE OF A JAVA CLASS

class HelloWorld{
public static void main(String args[ ]){
System.out.println(“Hello World”);
}
}

static – state of the method, it is static


because it must be called before the class
that hosts the method is instantiated

Fundamentals of Java Language


BASIC STRUCTURE OF A JAVA CLASS

class HelloWorld{
public static void main(String args[ ]){
System.out.println(“Hello World”);
}
}

void – It returns void because the Java


interpreter does not expect to receive or
process any output from the class.

Fundamentals of Java Language


BASIC STRUCTURE OF A JAVA CLASS

class HelloWorld{
public static void main(String args[ ]){
System.out.println(“Hello World”);
}
}

System.out.println( ) – The println method


is one of dozens of methods in the System
class. The System class is a part of the core
Java language package of the Application
Programming Interface (API)

Fundamentals of Java Language


Java Language Fundamentals

2.2

Fundamentals of Java Language


JAVA LANGUAGE FUNDAMENTALS

Ø Identifiers
Ø Keywords
Ø Comments
Ø Data types

Fundamentals of Java Language


Identifiers are names that are given by the
programmer as name of variables, methods
or functions, classes etc. The name used as
an identifier must follow the following rules
in Java™ technology:

ØEach character is either a digit, letter,


underscore or currency symbol.
ØFirst character cannot be a digit.
ØThe identifier name must not be a
reserved word.

Fundamentals of Java Language


Java keywords are reserved and cannot be used as identifiers.

abstract do import return void


boolean double instance of short volatile
break else int static while
byte extends interface super
case final long switch
catch finally native synchronized
char float new this
class for package throws
const goto private transient
continue if protected try
default public implements

Fundamentals of Java Language


Java Comments are of three (3) types:

1. A single-line comment starting with //

2. A multi-line comment enclosed within /* */

3. A documentation or javadoc comment is


enclosed between /** and */. These
comments can be used to generate HTML
documents using the javadoc utility, which is
part of Java language comment.

Fundamentals of Java Language


JAVA DATA TYPES

Primitive Data Types


Type Width (in bytes) Min Value Max Value
byte 1 -128 127
short 2 -32768 32767
int 4 -2147483648 21474833647
long 8 Long.MIN_VALUE Long.MAX_VALUE
float 4 Float.MIN_VALUE Float.MAX_VALUE
double 8 Double.MIN_VALUE Double.MAX_VALUE
boolean (1 bit) true false
char 2 (unsigned) ‘\u0000’ ‘\uFFFF’

Fundamentals of Java Language


Derived Data Types
String
Date
Integer
Double
Long
Float

Fundamentals of Java Language


VARIABLES

Ø Variable Declaration
Syntax: <datatype> <varName>; [= value;]
Example: String name;
int age;
double price = 55.66;
Ø Assigning a value
Syntax: <varName> = value;
Example: name = “Maria Blanco”;
age = 22;
price = 200.50;

Fundamentals of Java Language


WRAPPER CLASSES

Java Wrapper Classes are used in


converting one data type (such as a
String) into another data type (such as
int or double). It is also used in
wrapping a primitive value into an
object.

Fundamentals of Java Language


Wrapper Class Primitive Type
Integer int
Float float
Double double
Long long
Byte byte
Short short
Character char
Boolean boolean

Fundamentals of Java Language


Basic Input and Output

2.3

Fundamentals of Java Language


In Java, you can simply use

System.out.println(); or
System.out.print(); or
System.out.printf();

System is a class
out is a public static field: it accepts output data.
*We will discuss class, public, and static in later chapters.
class AssignmentOperator {
public static void main(String[] args) {
System.out.println("Java programming is interesting.");
}
}

Output:

Java programming is interesting.


Difference between println(), print() and printf()
print() - It prints string inside the quotes.
println() - It prints string inside the quotes similar
like print() method. Then the cursor moves to the beginning
of the next line.
printf() - Tt provides string formatting (similar to printf in
C/C++ programming).
class Output {
public static void main(String[] args) {
System.out.println("1. println ");
System.out.println("2. println ");

System.out.print("1. print ");


System.out.print("2. print");
}
}

Output:
1. println 2. println
2. 1. print 2. print
Java printf()
The Scanner class is used to get user input, and it is found in
the java.util package.

To use the Scanner class, create an object of the class and


use any of the available methods found in the Scanner class
documentation. In our example, we will use
the nextLine() method, which is used to read Strings:
import java.util.Scanner; // Import the Scanner class

public class MyClass {


public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username: ");
String userName = myObj.nextLine(); // Read user input
System.out.println("Username is: " + userName); // Output user input
}
}

Output:
Enter username: MyUsername
Username is: MyUserName
Scanner Input Types
Backiel, A. (2015). Beginning Java Programming: The Object-oriented Approach. 1st Edition
Fain, Y. (2015). Java programming 24-hour trainer. John Wiley and Sons:Indianapolis, IN
Smith, J. (2015). Java programs to accompany Programming logic and design. Cengage Learning:Boston, MA
Yener, M (2015). Professional Java EE Design Patterns. John Wiley & Sons, Inc.
Gordon, S. (2017). Computer Graphics Programming in opengl with Java. Mercury Learning and Information
Butler, A (2017). Java. s.n
Lowe, D (2017). Java all-in-one for dummies. John Wiley and Sons
Saumont, P (2017). Functional Programming in Java: How Functional Techniques Improve Your Java Programs. 1st Edition
Heffelfinger, D. (2017). Java EE 8 Application Development. Packt Puublishing
Murach, J. (2017). Murach’s Java Programming. Mike Murach & Associates,Inc.
Burd, B. (2017). Beginning programming with Java for dummies. John Wiley & Sons, Inc.
Manelli, L. (2017). Developing java web application in a day. s.n.
Klausen, P. (2017). Java 5: Files and Java IO software development (e-book)
Klausen, P. (2017). Java 3 object-oriented programming software development (e-book)
Muller, M (2018). Practical JSF in Java EE 8. Apress

You might also like