0% found this document useful (0 votes)
53 views

Chapter Two

This document provides an overview of basic Java programming concepts covered in Chapter 2, including: - Variable declaration and initialization, explaining the use of primitive data types like int, float, char, and boolean. - Input/output statements in Java, demonstrating how to get user input using Scanner and display output with System.out.println. - Operators in Java, covering arithmetic, relational, bitwise, and logical operators. - Type casting in Java, differentiating between implicit and explicit casting and providing examples of each. The chapter introduces fundamental Java building blocks like variables, data types, and operations to lay the groundwork for further programming concepts.

Uploaded by

Desyilal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Chapter Two

This document provides an overview of basic Java programming concepts covered in Chapter 2, including: - Variable declaration and initialization, explaining the use of primitive data types like int, float, char, and boolean. - Input/output statements in Java, demonstrating how to get user input using Scanner and display output with System.out.println. - Operators in Java, covering arithmetic, relational, bitwise, and logical operators. - Type casting in Java, differentiating between implicit and explicit casting and providing examples of each. The chapter introduces fundamental Java building blocks like variables, data types, and operations to lay the groundwork for further programming concepts.

Uploaded by

Desyilal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

CHAPTER TWO

BASICS IN JAVA GRAMMING


2

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,

characters, and Boolean types.


• These types are known as primitive data types or fundamental types.

Primitive data types


• Integer types: are types used to represent integer number only.

byte: a one byte signed integer data type.


4

Introduction to Java Program…


short: a two byte signed integer data type.
int : a four byte signed integer data type.
long: an eight byte signed integer data type.
•Floating- point types: are used to represent floating point numbers or
real numbers.
float: a four byte floating number data type
double: an eight byte floating number data type.
•Character data type
char: a two byte character data type
•Boolean algebra type
boolean: used to represent a Boolean value true or false
5

Introduction to Java Program…


• To let the compiler know about the variable, specify their data type known as

variable declaration.
• Variables should be declared before we use them.

• Syntax to declare a variable

• Identifier: the name of variable, method, class, constructors, interfaces

and packages.
• Java valid identifier should start with letter or underscore( _ ) or dollar

sig($) and can be followed by digit or letter.


6

Introduction to java prog…


Example: variable declaration

• The program declares radius and area as variables.


• The reserved word double indicates that radius and area are floating-point values
stored in the computer.
Variable initialization
• Initialization means assigning a value for a variable.
• For instance, if you want to initialize radius variable value; and compute the area.
Here is the complete program below.
7

Introduction to java prog…..


• ComputeArea java program
8

Introduction to java prog..

• javac command used to compile a java code


• java command to execute a java bytecode(compiled code)
9

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”.

public class Helloworld


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

Output Hello World


11

Output statem…..
• Example 2. Write a java program which display the sum of 100 and 300.

public class Sum


{
public static void main(String args[])
{
int num1=100;
int num2=300;
int sum;
sum=num1+num2;
System.out.print(“Result=”+sum);
}
}
• Output Result=400
12

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);
}
}

Output Enter the 1st number


5
Enter the 2nd number
6
Result=30
13

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

Operator precedence table


18
19

public class Operator


{
public static void main(String args[])
{
int res=10*5-2+6/2+7;
System.out.print(res);
}
}

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

• Explicit casting: conversion of data type should be defined


explicitly.
• Conversion of larger size data type to smaller size data type.
• E.g. conversion of float to int.
e.g. byte a=4;
Int b=(int) a;
• Syntax of explicit casting(float to int)
float a=10.2;
int b;
b=(int) a; //here the value of b will be 10

23

End of chapter two

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy