Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Basics Data Types in Java.

Similar presentations


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

1 Java Basics Data Types in Java

2 Data Types in Java Data type tells compiler:
How much memory to allocate Format in which to store data Types of operations you will perform on data

3 Data Types in Java Java is a strongly typed language Data types may be
Unlike C, Type checking is strictly enforced at run time Impossible to typecast incompatible types Data types may be Primitive data types Reference data types

4 Data Types in Java Primitive Numeric Integer int short long byte
Floating point float double Non-numeric char boolean Non-primitive Arrays Classes Interfaces

5 Data Types in Java Primitive Data Types in Java
Integer Data Types byte (1 byte) short (2 bytes) int (4 bytes) long (8 bytes) All numeric data types are signed The size of data types remain same on all platforms Char data type is 2 bytes as it uses the UNICODE character set. And so, Java supports internationalization Floating Data Types float (4 bytes) double (8 bytes) Character Data Types char (2 byte) Logical Data types boolean (1 bit) (true/false)

6 Integer Data Types in Java
Size in bytes Minimum Value Maximum Value 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,807

7 Declaration Example int testGrade;
int numPlayers, highScore, diceRoll; short xCoordinate, yCoordinate; byte ageInYears; long cityPopulation;

8 Floating Data Types in Java
Size in bytes Minimum Values Maximum Values Float 4 1.4E-45 E38 double 8 4.9E-324 E308

9 Declaration Example float salesTax; double interestRate;
double paycheck, sumSalaries;

10 Character Data Types in Java
Size in bytes Minimum Values Maximum Values Char 2 Encoded as 0 Encoded as FFFF One Unicode Character (16 bits - 2 bytes)

11 Declaration Example char finalScore; char newline, tab, doubleQuotes;

12 Logical Data Types in Java
boolean (1 bit) (true/false) Used for decision making or as "flag" variables

13 Declaration Example boolean isEmpty; boolean passed, failed;

14 Compatible Data Types Data Type Compatible Data Types byte byte
short byte, short int byte, short, int, char long byte, short, int, long, char float float, byte, short, int, long, char double float, double, byte, short, int, long, char boolean boolean char char

15 Data Types in Java Variables
Variables are defined in Java using the following format : <access modifier> <type> <variable name>; e.g. public String name; private int count; One value can be placed in one time in a variable but it may change syntax : dataType identifier; or dataType identifier1, identifier2, …;

16 Data Types in Java Variables
A named storage in the computer’s memory that stores a value of a particular type for use by program Example of variable declaration: The data type can either be: built-in primitive types (e. g. int, double, char) Reference data types (e.g. String, BufferedReader) Naming Convention: Variable Name: First word lowercase & rest with their initials capitalized (Camel Casing) e.g. thisIsALongVariableName DataType VariableName int myAge, cellPhone ; double salary ; char tempChar ;

17 Data Types in Java Variables (Contd…)
Using primitive data types is similar to other languages Variables can be declared anywhere in the program In Java, if a local variable is used without initializing it, the compiler will show an error int count ; int max=100; for (int count=0; count <max; count++) { int z = count * 10; } BEST PRACTICE: Declare a variable in program only when required Do not declare variables upfront like in C

18 Variable Naming Convention
First letter is lowercase Embedded words begin with uppercase letter Name of the variable should be reasonable and meaningful that depict the data store. Avoid extra large variable name. Similar Java keyword should not be used as variable name.

19 Assign Value to Variable
Variables Assignment operator = Value on the right of the operator is assigned to the variable on the left Value on the right can be a literal (text representing a specific value), another variable, or an expression (explained later) Syntax: dataType variableName = initialValue; Or dataType variable1 = initialValue1, variable2 = initialValue2, …;

20 Assign Value to Object Variables Assignment operator =
Value on the right of the operator is assigned to the variable on the left Value on the right can be a literal (text representing a specific value), another variable, or an expression (explained later) Syntax: dataType variableName = initialValue; Or dataType variable1 = initialValue1, variable2 = initialValue2, …;

21 Data Types in Java Comments in Java
A single line comment in Java starts with // A multi line comment starts with /* & ends with */ // This is a single line comment in Java /* This is a multi line comment in Java */

22 Data Types in Java Reference Data Types
Hold the reference of dynamically created objects which are in the heap Can hold three kinds of values: Class type: Points to an object / class instance Interface type: Points to an object which is implementing the corresponding interface Array type: Points to an array instance or “null” Difference between Primitive & Reference data types: Primitive data types hold values themselves Reference data types hold reference to objects, i.e. they are not objects, but reference to objects Objects & Arrays are accessed using reference variables in Java A reference variable Is similar to a pointer (stores memory address of an object) Java does not support the explicit use of addresses like other languages

23 Data Types in Java Reference Data Types (Contd…)
Java does not allow pointer manipulation or pointer arithmetic Memory Representation: primitive reference A reference type cannot be cast to a primitive type A reference type can be assigned ‘null- to show that it is not referring to any int primitive = 5; String reference = “Hello” ; 5 Hello

24 Data Types in Java Typecasting Primitive Data Types
Automatic type changing is known as Implicit Conversion - A variable of smaller capacity can be assigned to another variable of bigger capacity Whenever a larger type is converted to a smaller type, we have to explicitly specify the type cast operator This prevents any accidental loss of data int i = 10; double d; d = i; double d = 10; d = i; i = (int) d; Type cast operator ()

25 Access Modifier A modifier is a keyword placed in a class, method or variable declaration that changes how it operates. There are basically four types of access modifier in JAVA public private default protected

26 Default Modifier If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes.

27 Public Modifier A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. The following function uses public access modifier: Example : public static void main(String[] arguments) { / // ... }

28 Private Modifier The private modifier specifies that the member can only be accessed in its own class. Example: public class football{ private String score; public String getScore() { return this.score; } public void setScore(String score) { this.format = format;

29 Protected Modifier The protected modifier specifies that the member can only be accessed within its own package (as with package-private). Example: class AudioPlayer { protected boolean openSpeaker(Speaker sp) { // implementation details } } class StreamingAudioPlayer { boolean openSpeaker(Speaker sp) {

30 Protected Modifier Here, if we define openSpeaker() method as private, then it would not be accessible from any other class other than AudioPlayer. If we define it as public, then it would become accessible to all the outside world. But our intension is to expose this method to its subclass only, that's why we used protected modifier.

31 Sample Exercise #1 Is it legal to assign a char to an int variable ?
Yes, it is possible int i = ‘a’; // assigns 97 to I

32 Sample Exercise #2 Is it legal to assign an int to an char variable ?
Yes, it is possible char c = 97; // assigns ‘a’ to c

33 Sample Exercise #3 It is possible to perform arithmetic on char variables ? Yes, it is possible char ch = ‘a’; ch = ch + 1; // assigns ‘b’ to ch

34 Sample Exercise #4 Does a int value can be assigned to a char type variable ? Yes, we can assign a integer value to a char type variable but a char type value cannot be assigned to a int type variable


Download ppt "Java Basics Data Types in Java."

Similar presentations


Ads by Google