DSA Module 1 Intro To Java Programming 2weeks
DSA Module 1 Intro To Java Programming 2weeks
Unit 1
Introduction to JAVA Programming
Introduction
Presentation of Contents
JAVA BACKGROUND
The original motivation for Java was the need for platform independent
language that could be embedded in various consumer electronic products like
toasters and refrigerators. One of the first projects developed using Java was a
personal hand-held remote control named Star 7.
At about the same time, the World Wide Web and the Internet were
gaining popularity. Gosling et. al. realized that Java could be used for Internet
programming.
About Java programs, it is very important to keep in mind the following
points.
1
Unit 1: Introduction to JAVA Programming
Case Sensitivity - Java is case sensitive which means identifier Hello and
hello would have different meaning in Java.
Class Names - For all class names the first letter should be in Upper Case.
If several words are used to form a name of the class each inner words first
letter should be in Upper Case.
Method Names - All method names should start with a Lower Case letter.
If several words are used to form the name of the method, then each inner
word's first letter should be in Upper Case.
Program File Name - Name of the program file should exactly match the
class name.
When saving the file you should save it using the class name (Remember java
is case sensitive) and append '.java' to the end of the name. (if the file name
and the class name do not match your program will not compile).
public static void main(String args[]) - java program processing starts from
the main() method which is a mandatory part of every java program..
Dissecting my first Java program
Now, we’ll try to dissect your first Java program:
indicates the name of the class which is Hello. In Java, all code should be
placed inside a class declaration. We do this by using the class keyword. In
addition, the class uses an access specifier public, which indicates that our
class in accessible to other classes from other packages (packages are a
collection of classes). We will be covering packages and access specifiers
later.
2
Unit 1: Introduction to JAVA Programming
The next line which contains a curly brace { indicates the start of a block. In
this code, we placed the curly brace at the next line after the class declaration;
however, we can also place this next to the first line of our code. So we could
actually write our code as:
Or
The next three lines indicate a Java comment. A comment is something used
to document a part of a code. It is not a part of the program itself, but used for
documentation purposes. It is good programming practice to add comments to
your code.
/**
*My first java program
*/
A comment is indicated by the delimiters “/*” and “*/”. Anything within these
delimiters is ignored by the Java compiler, and are treated as comments. The
next line,
Indicates the name of one method in Hello which is the main method. The
main method is the starting point of a Java program. All programs except
Applets written in Java start with the main method. Make sure to follow the
exact signature.
Now, we learn two ways of creating comments. The first one is by placing the
comment inside /* and */, and the other one is by writing // at the start of the
comment.
3
Unit 1: Introduction to JAVA Programming
Prints the text “Hello world!” on the screen. The command
System.out.println(), prints the text enclosed by the quotation on the screen.
The last two lines which contain the two curly braces are used to close the
main method and class respectively.
Coding Guidelines:
1. Your Java program should always end with the .java extension.
2. Filenames should match the name of your public class. So, for
example, if the name of your public is Hello, you should save it in a
file called Hello.java.
3. You should write comments in your code explaining what a certain
class does, or what a certain method do.
System.out.println(“Hello world”);
Coding Guidelines:
1. In creating blocks, you can place the opening curly brace in line with
the statement, like for example,
or you can place the curly brace on the next line, like,
2. You should indent the next statement after the start of a block, for
example,
JAVA IDENTIFIERS
4
Unit 1: Introduction to JAVA Programming
All java components require names. Names used for classes, variables and
methods are called identifiers.
In java there are several points to remember about identifiers. They are as
follows:
5
Unit 1: Introduction to JAVA Programming
JAVA KEYWORDS
1. Logical - boolean
A boolean data type represents two states: true and false. An example
is,
2. Textual – char
A character data type (char), represents a single Unicode character. It
must have its literal enclosed in single quotes(’ ’). For example,
‘a’ //The letter a
‘\t’ //A tab
To represent special characters like ' (single quotes) or " (double quotes), use
the escape
character \. For example,
'\'' //for single quotes
'\"' //for double quotes
6
Unit 1: Introduction to JAVA Programming
String message=“Hello world!”
VARIABLES
Note: Values enclosed in <> are required values, while those values enclosed
in [] are optional.
7
Unit 1: Introduction to JAVA Programming
}
}
OPERATORS
Arithmetic Operators
Here are the basic arithmetic operators that can be used in creating
your Java programs,
8
Unit 1: Introduction to JAVA Programming
Aside from the basic arithmetic operators, Java also includes a unary
increment operator (++) and unary decrement operator (--). Increment and
decrement operators increase and decrease a value stored in a number variable
by 1.
For example, the expression
count = count + 1; //increment the value of count
by 1
is equivalent to,
count++;
Operator Use Description
++ op++ Increments op by 1; evaluates to the value
of op before it was incremented
++ ++op Increments op by 1; evaluates to the value
of op after it was incremented
-- op-- Decrements op by 1; evaluates to the value
of op before it was decremented
-- --op Decrements op by 1; evaluates to the value
of op after it was decremented
When the increment and decrement operators are placed after the operand, the
old value of the variable will be used in the expression where it appears. For
example,
int i = 10;
int j = 3;
int k = 0;
Example:
public class Variables
{
10
Unit 1: Introduction to JAVA Programming
public static void main(String[] args)
{
//a few numbers
int i = 37;
int j = 42;
double x = 27.475;
double y = 7.22;
System.out.println(" i++ = " + i++);
System.out.println(" --i = " + --i);
System.out.println(" --j = " + --j);
System.out.println(" x++ = " + x++);
}
}
Output:
i++ = 37
--i = 37
--j = 41
x++ = 27.475
Relational operators
11
Unit 1: Introduction to JAVA Programming
//greater than or equal to
System.out.println(" k >= j = " + (k >=
j)); //true
//equal to
System.out.println(" k == j = " + (k ==
j)); //true
//not equal to
System.out.println(" k != j = " + (k !=
j)); //false
}
}
12
Unit 1: Introduction to JAVA Programming
Logical operators
Logical operators have one or two boolean operands that yield a boolean
result.
Here's a sample source code that uses logical and boolean AND,
|| (logical OR)
13
Unit 1: Introduction to JAVA Programming
Here's a sample source code that uses logical OR,
! ( logical NOT)
The logical NOT takes in one argument, wherein that argument can be an
expression, variable or constant. Here is the truth table,
x1 Result
TRUE FALSE
FALSE TRUE
14
Unit 1: Introduction to JAVA Programming
class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a //Scanner object
System.out.println("Enter username");
15
Unit 1: Introduction to JAVA Programming
}
}
Input Types
In the example above, we used the nextLine() method, which is used to read
Strings. To read other types, look at the table below:
Method Description
In the example below, we use different methods to read data of various types:
Example
import java.util.Scanner;
class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
// String input
String name = myObj.nextLine();
// Numerical input
int age = myObj.nextInt();
double salary = myObj.nextDouble();
16
Unit 1: Introduction to JAVA Programming
// Output input by user
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}
17