java data types. data types in java data types primitive non primitive boolean characternumeric...
TRANSCRIPT
Java Data Types
Data Types in Java
Data Types
primitive Non primitive
Boolean Character Numeric
Integer Floating-point
charboolean byte short int long float double
Data Types in Java
byte:8-bit signed two's complement integer.Minimum value is -128 (-2^7)Maximum value is 127 (inclusive)(2^7 -1)Default value is 0Used to save space in large arrays, mainly
in place of integersExample: byte a = 100 , byte b = -50
Data Types in Java
short:16-bit signed two's complement integer. Minimum value is -32,768 (-2^15)Maximum value is 32,767 (inclusive) (2^15
-1)Used to save memoryDefault value is 0.Example: short s = 10000, short r = -20000
Data Types in Java
int:32-bit.Minimum value is -2^31Maximum value is (2^31 -1)Generally used as the default data type for
integral values unless there is a concern about memory.
The default value is 0.Example: int a = 100000, int b = -200000
Data Types in Java
long:64-bitMinimum value is (-2^63)Maximum value is (2^63 -1)This type is used when a wider range than
int is needed. Exam: light distance per yearDefault value is 0L.Example: long a = 100000L, long b = -
200000L
Data Types - long
class Light {public static void main(String args[]) {
int lightspeed;long days;long seconds;long distance;lightspeed = 186000L;days = 1000L; seconds = days * 24 * 60 * 60; distance = lightspeed * seconds; System.out.print("In " + days);System.out.print(" days light will travel about ");System.out.println(distance + " miles.");
}}
Data Types in Java
float:32-bit. Default value is 0.0f.Example: float f1 = 234.5f
Data Types in Java
double:64-bit.Default value is 0.0d.Example: double d1 = 123.4
As a decimal value multiplied by a power of 10. You can do this in Java by writing the number as a decimal value followed by an E, or an e, preceding the power of 10 that you require. For example,149,600,000 can be written as 1.496E8.
Data Types in Java
boolean:Represents one bit of information.Two possible values: true and false.Default value is false.Example: boolean one = true
Data Types in Java
char:16-bit Unicode character. Minimum value is '\u0000' (or 0).Maximum value is '\uffff' (or 65,535
inclusive).Char data type is used to store any
character.Example: char letterA ='A'
Data Types Example
public class AverageFruit {public static void main(String[] args) {
// Declare and initialize three variablesdouble numOranges = 50.0E-1; // Initial value is 5.0double numApples = 1.0E1; // Initial value is 10.0double averageFruit = 0.0;averageFruit = (numOranges + numApples)/2.0;System.out.println(“A totally fruity program”);System.out.println(“Average fruit is “ + averageFruit);
}}
OUTPUT:A totally fruity programAverage fruit is 7.5
Data Types in Java
Data Type Default Value Default size General Value
boolean false 1 bit true, false
char '\u0000' 2 byte A, b ...
byte 0 1 byte 100
short 0 2 byte (215-1) 500
int 0 4 byte 500
long 0L 8 byte 500
float 0.0f 4 byte 2.1
double 0.0d 8 byte 2.1
Data Types in JavaCategory Types
Size (bits)
Minimum Value
Maximum Value
Precision Example
Integer
byte 8 -128 127 From +127 to -128 byte b = 65;
char 16 0 216-1 All Unicode characterschar c = 'A';char c = 65;
short 16 -215 215-1 From +32,767 to -32,768 short s = 65;
int 32 -231 231-1From +2,147,483,647 to -2,147,483,648
int i = 65;
long 64 -263 263-1From +9,223,372,036,854,775,807 to -9,223,372,036,854,775,808
long l = 65L;
Floating-point
float 32 2-149 (2-2-23)·2127 From 3.402,823,5 E+38 to 1.4 E-45 float f = 65f;
double 64 2-1074 (2-2-52)·21023
From 1.797,693,134,862,315,7 E+308 to 4.9 E-324
double d = 65.55;
Otherboolean
1 -- -- false, trueboolean b = true;
Java ProgramWrite a program that display every data
type data value.
Reserved words in Java
abstract assert boolean break byte case
catch char class const* continue default
double do else enum extends false
final finally float for goto* if
implements import instanceof int interface long
native new null package private protected
public return short static strictfp super
switch synchronized this throw throws transient
true try void volatile while
special escape sequencesNotation Character represented
\n Newline (0x0a)
\r Carriage return (0x0d)
\f Formfeed (0x0c)
\b Backspace (0x08)
\s Space (0x20)
\t tab
\" Double quote
\' Single quote
\\ backslash
\ddd Octal character (ddd)
\uxxxxHexadecimal UNICODE character (xxxx)
Variable
Variable is name of reserved area allocated in memory.
int vData=50;
Types of Variable
There are three types of variables in java
local variable instance variable Class/static variable
Types of variables
class A{
int data=50; //instance variable static int m=100; //static variable void method()
{ int n=90; //local variable }
} //end of class
Local variables
Declared in methods or blocks
Destroyed once it exits the method or block
Access modifiers cannot be used
Visible only within the declared method or block
There is no default value for local variables so should initialized assigned before the first use.
Local variables
public class Test{
public void pupAge(){
int age = 0; // Test without initialized
age = age + 7;
System.out.println("Puppy age is : " + age);
}
public static void main(String args[]){
Test objTest = new Test();
objTest.pupAge();
}
}
Instance variables
Instance variables are declared in a class, but outside a method, constructor or any block
When a space is allocated for an object in the heap, a slot for each instance variable value is created.
Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed
Access modifiers can be given for instance variables.
Instance variables have default values
Instance variables
public class Employee{ public String name; private double salary; public Employee (String empName){ name = empName; } public void setSalary(double empSal){ salary = empSal; } public void printEmp(){ System.out.println("name : " + name ); System.out.println("salary :" + salary); } public static void main(String args[]){ Employee empOne = new Employee("Ransika"); empOne.setSalary(1000); empOne.printEmp(); }
}
Class/static variables
Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block
There would only be one copy of each class variable per class, regardless of how many objects are created from it.
Static variables are created when the program starts and destroyed when the program stops.
have default values.
Class/static variables
public class Employee{ // salary variable is a private static variable private static double salary;
// DEPARTMENT is a constant public static final String DEPARTMENT = "Development ";
public static void main(String args[]){ salary = 1000; System.out.println(DEPARTMENT + "average salary:" + salary); }
}
Mathematical functionsMath.abs(-1);Math.floor(1.9);Math.ceil(1.1);java.lang.Math.sqrt(4);Math.PI;Math.pow(5, 2);Math.random()
Mathematical functions
import java.lang.Math;
public class Program { public static void main(String[] args) {
// This version uses an int.int value = Math.abs(-1);System.out.println(value);
// This version uses a double.double value2 = Math.abs(-1.23);System.out.println(value2);
int value3 = Math.abs(1);System.out.println(value3);
}}
Output1 1.23 1
Mathematical functionsimport java.lang.Math;
public class Program { public static void main(String[] args) {
// These are all reduced, even the negative number.double floor1 = Math.floor(1.9);double floor2 = Math.floor(1.1);double floor3 = Math.floor(-1.3);
System.out.println(floor1);System.out.println(floor2);System.out.println(floor3);
}}
Output1.01.0-2.0
Programs for practics Variable
Sum of two integers* / %
Java Programs