Presentation is loading. Please wait.

Presentation is loading. Please wait.

5 Variables, Data Types.

Similar presentations


Presentation on theme: "5 Variables, Data Types."— Presentation transcript:

1 5 Variables, Data Types

2 Previously Hello World! Code Blocks Methods Statements
Style & Comments Simple Arithmetic class HiThere { /** * This is the entry point for an application. astrArgs the command line arguments. */ public static void main(String[] astrArgs) { // Starting of the main method System.out.println("Hello World!"); } // main() } // end class HiThere

3 Overview Variables: hold information Primitive Data Types
Other Data Types: Objects

4 Variables Symbolic representation of data of a specific type
Fields in Java Variables are named by an identifier Type must be declared before a variable can be used e.g. int iNum Values can be assigned to a variable Java assignment is = e.g. iNum = iCount; Variables can be modified during a programs execution (usually by assignment or operators)

5 Numeric Data Types Integer (int) Floating Point (float and double)
Whole numbers (e.g. 1, 2, 9, 923) no decimal part Floating Point (float and double) Decimals (e.g. 1.0, 3.14, ) NB Scientific Notation The letter 'E' means times 10 raised to the power ... Examples: 1E = 10 (i.e. 1*101) 4.6E+03 = 4600 (i.e. 4.6*103) 4.6E-03 = (i.e. 6*10-3)

6 Primitive Java Data Types
boolean true and false byte  1byte, -128 to 127 char  2bytes, from 0 to 65536 int  4bytes, from Integer.MIN_VALUE to Integer.MAX_VALUE long  8bytes, from Long.MIN_VALUE to Long.MAX_VALUE float  4bytes , from Float.MIN_VALUE to Float.MAX_VALUE double  8bytes , from Double.MIN_VALUE to Double.MAX_VALUE

7 Converting Types Types often need to be converted
Examples: Integer Real Real Integer String Real e.g. "3.14" Real String String class has methods to convert primitive to string; valueOf(data) which return an String String strValue = String.valueOf(10);  "10"

8 Converting Types All primitives have an equivalent class
boolean  Boolean long  Long char  Character float  Float int  Integer double  Double These classes are called wrapper class The conversion from a primitive to its wrapper class is called boxing From Java 1.5 the process is automatic -autoboxing Converting from a wrapper class to a primitive is called unboxing

9 Converting Types Wrapper classes are defined in the java.lang package
All equivalent classes of primitives have methods to covert from String parseXXX(String) to the primitive version XXX valueOf(String) to the equivalent class double dValue = Double.parseDouble("1.45"); Double value = Double.valueOf("1.45");

10 Converting Types Casting iNum = (int) dAmount
This forces dAmount to be treated as an int Syntactically any type can be cast into any other type. Be careful that it makes sense! If it does not, then the compiler might generate an error! Information that does not exist in the target type is lost e.g. lost of precision; (int) 1.5 is 1 so lost 0.5

11 Literals Syntactic representations of primitive types, or string data
Normally used to initialised variables class HiThere { /** * This is the entry point for an application. astrArgs the command line arguments. */ public static void main(String[] astrArgs) { // Starting of the main method System.out.println( "Hello World!" ); } // main() } // end class HiThere "Hello World!"

12 Objects Real-world objects share two characteristics: they all have state and behaviour State e.g. for a Torch - on / off Behaviour e.g. turn on / turn off Can be much more complex - e.g. Smartphone! In Java the state of an object is stored in fields (variables) and behaviour is expressed by methods

13 Objects A class defines the behaviour and characteristics (variables)
An Object has behaviour and state The variables have values that is referred as an state Changes in object variables mean a change in the state

14 Objects Class Object class Switch { Switch oSwitch = new Switch();
// true ON and false OFF boolean m_bOnOff = false; /** * Switches light on. */ public void switchOn() { m_bOnOff = true; } // switchOn() * Switches light off. public void switchOff() { m_bOnOff = false; } // switchOff() } // end Switch Switch oSwitch = new Switch(); oSwitch.switchOn(); oSwitch.switchOff(); false in m_b_OnOff = false; is a literal.

15 Packages Projects in Java are structured in packages
Packages are directories with subdirectories With .java files (development) With .class file (in .jar file) Name of package meaningful Windows Java src\com\aaz\sample0 src.com.aaz.sample0 src com aaz sample0 Windows view Eclipse view

16 Lets look at HelloWorld again!
Eclipse Lets look at HelloWorld again! Add packages


Download ppt "5 Variables, Data Types."

Similar presentations


Ads by Google