Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Data Types.

Similar presentations


Presentation on theme: "Java Data Types."— Presentation transcript:

1 Java Data Types

2 Data Types in Java Data Types Non primitive primitive Character
Numeric Boolean Floating-point Integer float double boolean char byte short int long

3 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 0 Used to save space in large arrays, mainly in place of integers Example: byte a = 100 , byte b = -50

4 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 memory Default value is 0. Example: short s = 10000, short r =

5 Data Types in Java int: 32-bit. Minimum value is -2^31
Maximum 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 = , int b =

6 Data Types in Java long: 64-bit Minimum 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 year Default value is 0L. Example: long a = L, long b = L

7 Data Types - long class Light {
public static void main(String args[]) { int lightspeed; long days; long seconds; long distance; lightspeed = L; 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."); }

8 Data Types in Java float: 32-bit. Default value is 0.0f.
Example: float f1 = 234.5f

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

10 Data Types in Java boolean: Represents one bit of information.
Two possible values: true and false. Default value is false. Example: boolean one = true

11 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'

12 Data Types Example public class AverageFruit { OUTPUT:
public static void main(String[] args) { // Declare and initialize three variables double numOranges = 50.0E-1; // Initial value is 5.0 double numApples = 1.0E1; // Initial value is 10.0 double 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 program Average fruit is 7.5

13 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 1 byte 100 short 2 byte (215-1) 500 int 4 byte long 0L 8 byte float 0.0f 2.1 double 0.0d

14 Data Types in Java Category Types Size (bits) Minimum Value
Maximum Value Precision Example Integer byte 8 -128 127 From +127 to -128 byte b = 65; char 16 216-1 All Unicode characters char c = 'A'; char c = 65; short -215 215-1 From +32,767 to -32,768 short s = 65; int 32 -231 231-1 From +2,147,483,647 to -2,147,483,648 int i = 65; long 64 -263 263-1 From +9,223,372,036,854,775,807 to -9,223,372,036,854,775,808 long l = 65L; Floating-point float 2-149 (2-2-23)·2127 From 3.402,823,5 E+38 to 1.4 E-45 float f = 65f; double 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; Other boolean 1 -- false, true boolean b = true;

15 Java Program Write a program that display every data type data value.

16 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

17 special escape sequences
Notation 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) \uxxxx Hexadecimal UNICODE character (xxxx)

18 Variable Variable is name of reserved area allocated in memory.
int vData=50;

19 Types of Variable There are three types of variables in java
local variable instance variable Class/static variable

20 Types of variables class A { } //end of class
int data=50; //instance variable   static int m=100; //static variable   void method()  int n=90; //local variable   }   } //end of class 

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

22 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();

23 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

24 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();

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

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

27 Mathematical functions
Math.abs(-1); Math.floor(1.9); Math.ceil(1.1); java.lang.Math.sqrt(4); Math.PI; Math.pow(5, 2); Math.random()

28 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); } Output 1 1.23

29 Mathematical functions
import 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); } Output 1.0 -2.0

30 Programs for practics Variable
Sum of two integers * / %

31 Java Programs


Download ppt "Java Data Types."

Similar presentations


Ads by Google