0% found this document useful (0 votes)
71 views28 pages

Object Oriented Programming From Datatypes

The document discusses various data types in Java including primitive and non-primitive types. It provides examples of primitive types like integers, floating-point numbers, characters, and booleans. It also discusses variable types in Java including local, instance, and static variables and provides examples of each. Finally, it covers various operators in Java including arithmetic, increment/decrement, bitwise, relational, and assignment operators and provides examples of how each can be used.

Uploaded by

ranjithdesigan
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)
71 views28 pages

Object Oriented Programming From Datatypes

The document discusses various data types in Java including primitive and non-primitive types. It provides examples of primitive types like integers, floating-point numbers, characters, and booleans. It also discusses variable types in Java including local, instance, and static variables and provides examples of each. Finally, it covers various operators in Java including arithmetic, increment/decrement, bitwise, relational, and assignment operators and provides examples of how each can be used.

Uploaded by

ranjithdesigan
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/ 28

OBJECT ORIENTED

PROGRAMMING
FROM DATATYPES
DATA TYPES
 Data types specify the different sizes and
values that can be stored in the variable.
There are two types of data types in Java:

◦ Primitive data types: The primitive data types


include Integer, Character, Boolean, and Floating
Point.

◦ Non-primitive data types: The non-primitive data


types include Classes, Interfaces, and Arrays.
DATA TYPES
 Java defines eight primitive types of data: byte, short,
int, long, char, float, double, and boolean. These can
be put in four groups:
 Integers This group includes byte, short, int, and long,
which are for whole-valued signed numbers.
 Floating-point numbers This group includes float and
double, which represent numbers with fractional
precision.
 Characters This group includes char, which represents
symbols in a character set, like letters and numbers.
 Boolean This group includes boolean, which is a
special type for representing true/false values.
EXAMPLES
 // Compute distance light travels using long variables.
 class Light {
 public static void main(String args[])
 {
 int lightspeed;
 long days;
 long seconds;
 long distance;
 // approximate speed of light in miles per second
 lightspeed = 186000;
 days = 1000;
 // speci
 fy number of days here seconds = days * 24 * 60 * 60; // convert to seconds distance =
lightspeed * seconds;
 // compute distance S
 System.out.print("In " + days);
 System.out.print(" days light will travel about ");
 System.out.println(distance + " miles.");
 }
 }
OUTPUT
 In 1000 days light will travel about
16070400000000 miles. Clearly, the result
could not have been held in an int variable.
VARIABLE
 A variable is a container which holds the
value and that can be changed durig the
execution of the program. A variable is
assigned with a datatype. Variable is a name
of memory location. All the variables must be
declared before they can be used. There are
three types of variables in java: local variable,
instance variable and static variable.
Local Variable

 A variable defined within a block or


method or constructor is called local
variable.
◦ These variable are created when the block in
entered or the function is called and destroyed
after exiting from the block or when the call
returns from the function.
◦ The scope of these variables exists only within
the block in which the variable is declared. i.e. we
can access these variable only within that block.
 import java.io.*;
 public class StudentDetails
 {
 public void StudentAge()
 { //local variable age int age = 0;
 age = age + 5;
 System.out.println("Student age is : " + age);
 }
 public static void main(String args[])
 {
 StudentDetails obj = new StudentDetails(); obj.StudentAge();
 }
 }
 OUTPUT:
 Student age is : 5
INSTANCE VARIABLE
 Instance variables are non-static variables and
are declared in a class outside any method,
constructor or block.

◦ As instance variables are declared in a class, these


variables are created when an object of the class is
created and destroyed when the object is destroyed.

Unlike local variables, we may use access
specifiers for instance variables. If we do not specify
any access specifier then the default access specifier
will be used.
 import java.io.*; class Marks{
 int m1; int m2;
 }
 class MarksDemo
 {
 public static void main(String args[])
 { //first object
 Marks obj1 = new Marks(); obj1.m1 = 50;
 obj1.m2 = 80;
 //second object
 Marks obj2 = new Marks(); obj2.m1 = 80;
 obj2.m2 = 60;
 //displaying marks for first object System.out.println("Marks for first
object:"); System.out.println(obj1.m1); System.out.println(obj1.m2);
 //displaying marks for second object System.out.println("Marks for second
object:"); System.out.println(obj2.m1); System.out.println(obj2.m2);
 }}
OUTPUT
 Marks for first object:
 50
 80
 Marks for second object:
 80
 60
STATIC VARIABLE
 Static variables are also known as Class
variables.
◦ These variables are declared similarly as instance
variables, the difference is that static variables are
declared using the static keyword within a class
outside any method constructor or block.
◦ Unlike instance variables, we can only have one
copy of a static variable per class irrespective of how
many objects we create.
◦ Static variables are created at start of program
execution and destroyed automatically when
execution ends.
EXAMPLE
 import java.io.*; class Emp {
  
 // static variable salary public static double salary;

public static String name = "Vijaya";


 }
 public class EmpDemo
 {
 public static void main(String args[]) {
  
 //accessing static variable
 without object Emp.salary = 1000;
 System.out.println(Emp.name + "'s average salary:" + Emp.salary);
 }
 }
 Output:
 Vijaya’s average salary:10000.0
  
OPERATORS IN JAVA

 Java provides a rich set of operators to


manipulate variables. We can divide all the
Java operators into the following groups –
 Arithmetic Operators
 Increment and Decrement
 Bitwise Operators
 Relational Operators
 Boolean Operators
 Assignment Operator
 Ternary Operator
Arithmetic Operators
Example:
 // Demonstrate the basic arithmetic operators. class BasicMath
 {
 public static void main(String args[])

 {
 // arithmetic using integers

 System.out.println("Integer Arithmetic"); int a = 1 + 1;

 int b = a * 3;
 int c = b / 4;

 int d = c - a;

 int e = -d;
 System.out.println("a = " + a);

 System.out.println("b = " + b);

 System.out.println("c = " + c);

 System.out.println("d = " + d);

 System.out.println("e = " + e);


 // arithmetic using doubles

 System.out.println("\nFloating Point Arithmetic"); double da = 1 + 1;

 double db = da * 3; double dc = db / 4; double dd = dc - a; double de = -dd;


 System.out.println("da = " + da);

 System.out.println("db = " + db); System.out.println("dc = " + dc);

 System.out.println("dd = " + dd); System.out.println("de = " + de);

 }}
Output:
 Integer Arithmetic a = 2
 b=6
 c=1
 d = -1
 e=1
 Floating Point Arithmetic da = 2.0
 db = 6
 dc = 1.5
 dd = -0.5
 de = 0.5
Modulus Operator

 The modulus operator, %, returns the remainder of a division


operation. It can be applied to floating-point types as well as
integer types.
 Example:
 // Demonstrate the % operator.
 class Modulus
 {
 public static void main(String args[])
 { int x = 42;
 double y = 42.25;
 System.out.println("x mod 10 = " + x % 10); System.out.println("y
mod 10 = " + y % 10);
 }
 }
Output:

 x mod 10 = 2
 y mod 10 = 2.25
Arithmetic Compound Assignment Operators

 Java provides special operators that can be


used to combine an arithmetic operation with
an assignment.
 a = a + 4;
 In Java, you can rewrite this statement as

shown here: a += 4;
 Syntax:
 var op= expression;
 Example:
 // Demonstrate several assignment operators. class OpEquals
 {
 public static void main(String args[])
 {
 int a = 1; int b = 2; int c = 3; a += 5;
 b *= 4;
 c += a * b;
 c %= 6;
 System.out.println("a = " + a); System.out.println("b = " + b);
System.out.println("c = " + c);
 }
 }
 Output:
 a = 6
 b = 8
 c = 3
Increment and Decrement Operators

 The ++ and the – – are Java’s increment and decrement operators. The increment
operator increases its operand by one. The decrement operator decreases its operand
by one.

 Example:
 // Demonstrate ++. class IncDec
 {
 public static void main(String args[])
 {
 int a = 1; int b = 2; int c;
 int d;
 c = ++b; d = a++; c++;
 System.out.println("a = " + a);
 System.out.println("b = " + b);
 System.out.println("c = " + c);
 System.out.println("d = " + d);
 }
 }
 Output:
 a = 2
 b = 3
 c = 4
 d = 1
Bitwise Operators

 Java defines several bitwise operators that


can be applied to the integer types: long, int,
short, char, and byte. These operators act
upon the individual bits of their operands.

You might also like