Data Types and Variables

Information about Data Types and Variables

Published on July 29, 2014

Author: nantha98

Source: authorstream.com

Content

PowerPoint Presentation: 1 Data Types and Variables by Nantha Kumar Subramaniam PhD CITP Learning Outcomes: Learning Outcomes At the end of the lesson, you should be able to: Describe the data types supported in Java; Declare variables; and Perform assignment statement in Java. Computer Programs: Overview: Computer Programs: Overview A computer program is a sequence of instructions that a computer can execute to perform a task There are TWO basic blocks of program: data and instructions In order to work with the instructions , you need to understand control structures and methods In order to work with data , you need to understand data types and variables In this lesson, we will focus on data types and variables Its related concepts such operators and multiple assignment will be covered in other lesson Data Types (1/5): Data Types (1/5) The integer type of data is used for numbers that do not have decimals. Negative values are also allowed. Java prepares four types of integer data, namely, int , short , long and byte . The integers data types of int , short , long and byte are differentiated by their storage size and value range as shown in the table below: Data Types (2/5): Data Types (2/5) The real data type or floating point represents numbers that have the decimal parts. There are two types of real data types, namely, float and double . The float and double data types are differentiated by their storage size and value range as shown in the table below: Data Types (3/5): Data Types (3/5) The character data type is represented by char . The single open inverted sign ( ‘ ) is used to show character constant. As an example: ‘ Y ’ shows character constant. Data Types (4/5): Data Types (4/5) Boolean types have two values: true and false . These literals are typed without quotes, but they represent values, not variables. Boolean-valued expressions are used extensively in control structures and to test the mantic expressions. Boolean values can also be assigned to variables of type boolean . Data Types (5/5): Data Types (5/5) In addition to the eight primitive data types listed above, the Java programming language also provides special support for strings Enclosing your character string within double quotes will automatically create a new String object; For example String s = “United Nations"; However, the String class is not classified as a primitive data type Variables: Variables What is variable? Programs manipulate data that is stored in memory. In machine language, data can only be referred to by giving the numerical address of the location in memory where it is stored. In a high-level language such as Java, names are used instead of numbers to refer to data. It is the job of the computer to keep track of where in the memory is the data actually stored; the programmer only has to remember the name. A name used in this way to refer to data stored in memory is called a variable. Example: name, age, address Variable Declaration (1/3): Variable Declaration (1/3) Variable must be declared before it can be used in a program Format of variable declaration: <data type> <variable name> ; Example: int age ; double height ; char grade ; 10 Variable Declaration (2/3): Variable Declaration (2/3) Java allows variables of the same data type to be declared in one line as shown below by using comma: Example: int age , ID , houseNumber ; 11 Variable Declaration (3/3): Variable Declaration (3/3) 12 A variable in Java is designed to hold only one particular type of data; it can legally hold that type of data and no other. Variable must be declared first before it can be used. Variable Naming Rules: Variable Naming Rules The rules of naming of variable: Variable names are case-sensitive A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_" White space is not permitted. Variable name must not be a reserved word such as int , class , static , etc 13 Variable Naming Conventions: Variable Naming Conventions The conventions of naming of variable: If possible, give meaningful names such as name instead of naming it as n that does not give any meaningful name The convention, however, is to always begin your variable names with a letter, not "$" or "_" If the name you choose consists of only one word, spell that word in all lowercase letters If it consists of more than one word, capitalize the first letter of each subsequent word. The names newValue and currentMarks are examples of this convention. 14 Invalid Variable Names: Invalid Variable Names my name 2age this 15 has white space cannot start with a digit “this” is a reserved word in Java Valid Variable Names: Valid Variable Names my_name _age id2 AGE 16 Variable Default Values: Variable Default Values When a variable is declared, a default value will be assigned to it Different data type will have different default values as shown in the table below: 17 Example: : Example: int age ; 18 age 0 “0” is the default value for int data type Effect in the computer memory: Initial Values (1/2): Initial Values (1/2) If you do not want the variable to be assigned the default value, you can assign you own initial value to the variable during the declaration Assigning a value to a variable is discussed in the next slide 19 Initial Values (2/2): Initial Values (2/2) An initial value can be assigned to a variable during its declaration Example: int age = 20 ; float average = 30.4 f ; If no initial value is assigned during the variable declaration, the default value of the data type will be used by the system 20 Note: The character “ f ” must be written after the initial value of the float data types Assigning Value to Variable (1/3): Assigning Value to Variable (1/3) A variable value also can be changed/modified as its declaration This is discussed in the next slide 21 Assigning Value to Variable (2/3): Assigning Value to Variable (2/3) In Java, the only way to get data into a variable is with an assignment statement. An assignment statement in Java has the following form: variable = expression ; Here expression refers to data or its computation whereas variable is any name of variable that has been declared The expression can consist of any variable , constant , expression or a combination of the three that is linked by the operator such as + , - , / , * and others that can produce a value When the computer comes to an assignment statement when executing a program, it evaluates the expression and puts the resulting data value into the variable 22 Assigning Value to Variable (3/3): Assigning Value to Variable (3/3) For example, consider the simple assignment statement: discount = 100 ; The variable in this assignment statement is discount , and the expression is the number 100 . The computer executes this assignment statement by putting the number 100 in the variable discount , replacing whatever was there before. Now, consider the following assignment statement, which might come later in the same program: newPrice = oldPrice -discount; Here, the value of the expression " oldPrice -discount " is being assigned to the variable newPrice . In the expression, the – is a " substraction operator" that tells the computer to subtract the value of discount from the oldPrice . The names oldPrice and discount are themselves variables, and it is the values stored in those variables that are to be computed 23 Valid Assignment Statements: Valid Assignment Statements data = 200 ; discount = oldPrice * (20 / 100) ; z = x + y ; 24 Assigning Value to Variable-Example: Assigning Value to Variable-Example The value of variable can change in its lifetime as shown in the example below: int a=1, b ; //Line 1 a=12 ; //Line 2 b=3*a ; //Line 3 The effect in the computer memory when Line is being executed: 25 a b 1 2 = 3*a =3*12= 36 3 1 12 0 36 Final state of the variables Constant: Constant The variable whose value is always constant, which means it does not change, is known as constant The reserved word final is used to declare constants Format: public static final data_type contanst_name = constant_value; Example: public static final double PI = 3.14 ; 26 PowerPoint Presentation: 27 Thank you

Related presentations


Other presentations created by nantha98

Using access modifiers
07. 07. 2014
0 views

Using access modifiers

Object and classes
09. 07. 2014
0 views

Object and classes

09. 07. 2014
0 views

"while" loops

switch statements
09. 07. 2014
0 views

switch statements

GUI-based Applets
07. 07. 2014
0 views

GUI-based Applets

Java Operators
29. 07. 2014
0 views

Java Operators