Lesson 2 JAVA Data Types Variables Classes and

Lesson 2: JAVA Data Types, Variables, Classes and Objects INTRODUCTION TO JAVA PROGRAMMING

Program Structure • A Java program consists of: – Classes (one or more interdependent) – which describe the properties and capabilities of the objects the program has to deal with – within the class • Fields describe the properties or attributes • Methods describe the capabilities, behaviour or operations

Classes vs Objects • Class: – Blueprint or template for an object – The ‘type’ of an object • Object: – An instance of a class • Classes and objects can represent conceptual (abstract) or physical (concrete) entities

What is an Object? • Student • Car • Document • GUI button • ++

What isn’t an Object? • Attributes of an object – Speed, colour, make would be attributes of a car object • Operations of an object – Turn left, speed up, slow down, turn right are all possible operations of a car object

Method A named sequence of instructions • The executable part of the program • For now we will use two kinds of instruction: – Invocation (of a method), eg: System. out. println(“Hello World!”); – Assignment (of values to variables), e. g: a = 10;

Types • Java is strongly typed and strongly classed • Every field, variable and expression has a type or a class • Only fields with the same types or classes can be used together • There are 8 primitive or Simple types , These Can be put in four groups

Primitive types

Types Groups • Integers • Floating-Point Numbers • Characters • Boolean

Integers • Four Integer types in Java Name Width Range -byte 8 -128 to 127 -short 16 -32768 to 32767 -int 32 -2, 147, 483, 648 to 2, 147, 483, 647 -long 64 9, 223, 372, 036, 854, 775, 808

Floating Point Types • Floating Point Numbers are also known as Real numbers. • Two types of floating point types Float and Double Name Width Range double 64 1. 7 e-308 to 1. 7 e+308 float 32 3. 4 e-038 to 3. 4 e+038

Characters • This Data type store the character char. • No Negative chars char ch 1, ch 2, ch 3; Character declaration ch 1=‘Z’; and ch 2=‘z’; variable Or Ch 3 =88; Character stored Code for X

BOOLEAN • Only one of two possible values, True or False boolean b; b=true; or b=false; At one time only one could be Possible. Usually used for Comparison and Flagging.

Strings • Strings are not a primitive type in Java • String is a class; Strings are objects • But they are so common that Java provides language level support for them • String literals are delimited by double quote marks, eg. “Hello World!”

Variables • Variable is basic named unit of storage and it stores a Value. • Variables may be declared anywhere in a class or method • Format: type name 1, name 2; type name 1 = initvalue; • Example: int i; Declaration int i=3; Assignment or Initialisation

Constants • format: static final type name = value; • static indicates a class field, that is, constant declarations occur at class level not inside a method • final means that the contents of the field cannot be changed • example: static final double BOILINGPOINT = 100. 0;

Assignments • standard assignment operator is = • format: variable = expression; • expression can be a mathematical formula or the result of a condition (eg. i < 100) • example: i = 5; i=i+j; less. Than. Century = (i < 100); • When you declare a variable, you create a named storage location. • When you make an assignment to a variable, you give it a value.

Operators NUMERICAL Operators + Addition Subtraction / Division * Multiplication % Modulus(Remainder) variable++ Increment by 1 variable-Decrement by 1 Example: 1+1 hour-1 hour*60 + minute/60

Order of Operators (Precedence) • Multiplication and division take precedence (happen before) addition and subtraction. Example: 6*3 -1 =? 6/3 -1 =? • Same Precedence evaluated from Left to Right • Use Parentheses when Unsure about Precedence rule (minute * 100) / 60

In Place Assignment Operators += -= *= /= %= • example: count += 5; • is equivalent to: count = count + 5;

Increment and Decrement • post-increment: a = 5; b = a++; // b ==5; a ==6; • pre-increment: a = 5; b = ++a; // b ==6; a ==6; • also pre- and post-decrement operators
- Slides: 21