Chapter Two
Chapter Two
Outline
• Introduction to programming in java
• Input & output statement in java
• Comments
• Operators in java
• Type conversion/casting
• Naming convention
3
Introduction to Java
Programming
• Variable represents a value stored in the computer’s memory.
• Variable name has to be chosen descriptive names: like radius for radius,
and area for area variables. Bad variable name x,a,y etc.
• Every variable has name, type, size and value.
• Java provides simple data types for representing integers, real numbers,
variable declaration.
• Variables should be declared before we use them.
and packages.
• Java valid identifier should start with letter or underscore( _ ) or dollar
Input/Output statements
• Output statements in java used to display results to the user.
• The basic output statements in java are:
System.out.println(“Hellow world”);
System.out.print(“Hellow world”);
JOptionPane.showMessageDialogue(null, “Hello World”);
• Input statements used to accept data from the user.
• Examples of Input statements in java can be implemented using
BufferedReader class(read(); readLine(); methods)
Scanner class (next(), nextInt(),nextFloat()…methods)
10
I/O statements…
• Example 1: Write a java program which display “Hello World”.
Output statem…..
• Example 2. Write a java program which display the sum of 100 and 300.
Input statem…
• Example 1: write a java program which accept two number from the user and display their product.
(using scanner class)
Import java.util.*;
public class Product
{
public static void main(String args[])
{
int num1,num2,prod;
Scanner input = new Scanner(System.in);
System.out.println(“please enter the 1st number”);
num1=input.nextInt();
System.out.println(“please enter the 2nd number”);
num2=input.nextInt();
prod=num1*num2;
System.out.print(“Result=”+prod);
}
}
Input statement
import java.io.*;
public class Sum{
public static void main(String args[]){
int num1,num2,sum;
BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
System.out.println(“please enter the 1st number”);
num1=input.read();
System.out.println(“please enter the 2nd number”);
num2=input.read();
sum=num1+num2;
System.out.print(“Result=”+sum);}
}
Output Enter the 1st number
5
Enter the 2nd number
6
Result=11
Comparative Study of Programming Languages Joey Paquet, 2010-2014 14
Constants
• Constant variable has value which can’t be changed.
• In java constant variables can be created by using “final”
key word before data type.
• Syntax
• final type varname;
e.g. final int age=20;
final float pi=3.147;
Comparative Study of Programming Languages Joey Paquet, 2010-2014 15
String
• String is a class in java used to represent a sequence of
characters/ a group of characters.
• To declare a string variable in java, you should have to
use String keyword.
• String variables value should be enclosed with double
quote(“ ”).
• E.g. String name=“Abebe”;
• String degree=“MSc.”
• A String class contains different methods for manuplation
and comparision the string like
subString(),toLowerCase(),toUpperCase(),trim(),equal
s()……
16
Operators in java
Java supports four kinds of operators. These are
•1. Arthimetic (e.g. addition(+), subtraction(-), division(/), modulus(%),
multiplication(*))
•2. Relational(less than(<),greater than(>),equal to(==),not equal to(!=),less
than or equalto(<=) and greater than or equalto(>=)
•3. Bitwise(and(&),or(|), xor(^),left shift(<<), right shift(>>)
•4. Logical (and(&&),or(||),short circuit and(&), short circuit or(|))
Operator precedence
The operator precedence order specifies which operator should be executed
first and which should be last.
17
Output=58
20
21
Type casting
•Java is a strongly typed programming language.
•Which mean that you cant assign one data type variable to
another implicitly. E.g. you cant assign an integer number
for floating point variables.
•There are two kinds of casting data type in java
•Implicit vs Explicitly
•Implicit casting: casing is done implicitly.
•Conversion of small size data type to larger size type.
•E.g. conversion of int to long.
22