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

Week 1

Java course Week 1

Uploaded by

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

Week 1

Java course Week 1

Uploaded by

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

W EEK

1 OBJECT
ORIENTED
PROGRAMMI
NG
IN
JAVA Instructor:
Nazish Basir
TOPICS
• INTRODUCTION TO OBJECT
ORIENTED PROGRAMMING
• I N T R O D U C T I O N T O J AVA
• I N S TA L L AT I O N O F J AVA
• GENERAL STRUCTURE OF
PROGRAM
• RUNNING YOUR FIRST
PROGRAM
• VA R I A B L E S A N D D ATA T Y P E S
• LITERALS
OBJECT ORIENTED
PROGRAMMING
• Programming paradigm based on the concept of
objects, which may contain data, in the form
of fields, often known as attributes; and code,
in the form of procedures, often known
as methods.
• Object-Oriented Programming (OOP) is a
programming language model, which
uses Objects and data as core components.
• The main idea behind OOP is to represent the
data and logic with objects instead of actions or
functions.
CLASS
• The class defines the shape and nature of an
object.
• A class is the template or blueprint from which
objects are actually made.
• A class is also defined as a new data type, a
user defined type which contain two things:
– Data Member
– Methods
• A class defines the properties and behaviors for
objects.
• The description of a number of similar object is
OBJECT
• An object is an instance of a class.
• An object represents an entity in the real world
that can be distinctly identified. For example, a
student, a desk, a circle, a button, and even a
loan can all be viewed as objects.
• Object to object communication is done via
methods.
OBJECT
• Software objects also have a state and a
behavior:
– The state of an object (also known as its properties or
attributes):
– is represented by data fields with their current values.
• A circle object, for example, has a data field radius , which is
the property that characterizes a circle.
• A dog has states -- color, name, breed
– The behavior of an object (also known as its actions):
– is defined by methods.
• To invoke a method on an object is to ask the object to perform
an action.
JAVA
• Object-oriented programming language
• Resembles C++ in many respects.
• Architecture-neutral means Java program should be able
to run on a any Operating System without recompilation.
• Java programs are compiled into machine-
independent byte code which is then run within a Java
interpreter, which is responsible for executing the byte
code instructions.
• The Java interpreter is typically referred to as the Java
Virtual Machine (JVM), and it must be present on each
computer that runs the program.
COMPILING JAVA
Compiler Interprete
r
Source Code Byte Code
JAVAC JAVA Execution
(.java) (.class)
ASSIGNMENT
• Write a 2 page report about Java
History.
• Include how java changed over
time by including versions and
releases.
INSTALLING JAVA
AND WRITING AND
COMPILING YOUR
FIRST PROGRAM
VARIABLES
• Named locations in memory where you
can store values.
• Can store one data value at a time
• Can change the values
• Variable Names (Identifiers) can be
short names (like x and y) or more
descriptive names (age, sum,
totalVolume)
DECLARING VARIABLES
• Declaring means giving a name
and a data type to a variable
before it can be used.
• Syntax:
datatype identifier;
datatype identifier1,
identifier2, ..;
INTEGER DATA TYPES

Type Size Min Value Max Value


(Bytes)
byte 1 -128 127

short 2 -32,768 32,767

int 4 - 2,147,483,648 2,147,483,647

long 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807


FLOATING-POINT DATA
TYPES
Size
Max Positive Non
Type (Byte Max Value
Zero Value
s)
float 4 1.40239846 x 10-45 3.40282347 x 1038
doubl 8 4.9406564584124654 x 1.7976931348623157 x
e 10-324 10308
CHARACTER DATA TYPE
char
• Size: 2 Bytes
• Min Value: The character encoded
as 0000, the null character
• Max Value: The value FFFF which
is a special code for “not a
character”
BOOLEAN DATA TYPE
boolean

• Can store only two values, which


are expressed using the Java
reserved words true and false.
THE ASSIGNMENT OPERATOR
AND INITIAL VALUES

• When you declare a variable, you can also assign an


initial value to the databy using the assignment
operator (=)

datatype variableName = initialValue;

datatype var1=initialValue1, var2=initialValue2 ;

• Notice that assignment is right to left. The initial


value is assigned to the variable.
IDENTIFIERS RULES
• Names can contain letters, digits, underscores ( _ ), and
dollar ( $ ) signs
• Names should begin with a letter
• Names can also begin with $ and _
• Names are case sensitive ("myVar" and "myvar" are
different variables)
• Names should start with a lowercase letter and it
cannot contain whitespace. If the variable name
consists of more than one word, then each word after
the first should begin with a capital letter. For example,
these identifiers are conventional Java variable names:
number1, highScore, booksToRead, ageInYears, and
LITERALS
• INTEGER LITRALS:
• Can be expressed in decimal(base 10)
• Hexadecimal(base 16) : Prefix 0 is used to indicate octal
• Octal(base 8) : prefix 0x indicates hexadecimal
• Examples:
int decimal = 100;
int octal = 0144;
int hexa = 0x64;
• Values of type long that exceed the range of int can be
created from long literals by specifying L or l:
long var = 1234567654321L
LITERALS
• FLOATING-POINT LITERALS
• A floating-point literal is of type float if it ends with
the letter F or f; otherwise its type is double and it
can optionally end with the letter D or d.
• The floating point types (float and double) can also
be expressed using E or e (for scientific notation),
double d1 = 123.4;
• same value as d1, but in scientific notation
double d2 = 1.234e2;
float f1 = 123.4f;
LITERALS
• CHAR LITERALS
• Single quote :
char ch = ‘N';
• Integral literal: Unicode value in Decimal, Octal and
Hexadecimal.
char ch = 78; //range is 0 to 65535
char ch = 0116; //range 000000 to 177777
char ch = '\u004E'; // range \u0000 to \uFFFF
• Escape Sequence :
char ch = '\n';
JAVA ESCAPE SEQUENCE
Escape
Sequen Description
ce
\t Insert a tab in the text at this point.
\b Insert a backspace in the text at this point.
\n Insert a newline in the text at this point.
\r Insert a carriage return in the text at this point.
\f Insert a formfeed in the text at this point.
Insert a single quote character in the text at this
\'
point.
Insert a double quote character in the text at
\"
EXERCISE 1:
Write a program which displays information about
you like: Your Name, Fathers Name, Class Roll No.,
age, and Cell Phone number.

EXERCISE 2:
Write down a program which has 2 variable ‘a’ and
‘b’ assign them any value and evaluate the
following equation:
x = a2 + 2ab + b2
Create variable x and Display the result of ‘x’
EXERCISE 3:

 Declare two short variable and try to assign their sum to byte variable and write
down what happened.
short a=10, b=20;
short c = a+b;
 Declare a long variable with value with int range then declare another long
variable with value outside the range of int. Write down what happened.
 Declare a float variable like this and write down what happened.
float a = 12.54;
 Declare all types of integers and floating point variables and try to give those
values outside of their range and then write down the errors.
 Create 5 character variable by giving it different hexadecimal codes and write
down what characters are shown on screen.
 Declare and initialize two character variable with any alphabet value. Then use
this statement to print the result and write down what happened.
System.out.println (ch1 + ch2);
END

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