0% found this document useful (0 votes)
22 views

TQA2023 2024 JavaVariablesTypes Part1

This document discusses Java variables and data types. It defines variables as names that hold memory locations for data values. Variables must be declared with a data type and can be assigned values that may change. The document outlines Java's primitive data types including integer, floating-point, boolean, and character types. It provides examples of declaring and assigning values to variables and discusses type compatibility checking in expressions.

Uploaded by

Ley Manalo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

TQA2023 2024 JavaVariablesTypes Part1

This document discusses Java variables and data types. It defines variables as names that hold memory locations for data values. Variables must be declared with a data type and can be assigned values that may change. The document outlines Java's primitive data types including integer, floating-point, boolean, and character types. It provides examples of declaring and assigning values to variables and discusses type compatibility checking in expressions.

Uploaded by

Ley Manalo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Java Identifiers and Data

Types
ICT Grade 10
Variables
• What is a variable?
• The name of some location of memory used to hold a data value
• Different types of data require different amounts of memory. The compiler’s job
is to reserve sufficient memory
• Variables need to be declared once
• Variables are assigned values, and these values may be changed later
• Each variable has a type, and operations can only be performed between
compatible types

3 width
• Example 4 height
int width = 3; 12 area
int height = 4;
int area = width * height;
width = 6;
area = width * height;
6 3 width
4 height
24 12 area
Data declaration syntax
◇The syntax for the declaration of a variable is:
◇Data type identifier;
◇“data type” may be the name of a class, as we have seen,
or may be one of the simple types, which we’ll see in a
moment
◇“identifier” is a legal Java identifier; the rules for simple
variable identifiers are the same as those for object
identifiers
Variable declaration: examples
◇For example:
int age; // int means integer
double cashAmount; // double is a real #
◇We can also declare multiple variables of the same type
using a single instruction; for example:
int num1, num2, num3; // or
int num1,
num2,
num3;
◇ The second way is preferable, because it’s easier to
document the purpose of each variable this way.
Identifiers
◇An identifier is a sequence of characters that consist of
letters, digits, underscores (_), and dollar signs ($).
◇An identifier must start with a letter, an underscore
(_), or a dollar sign ($). It cannot start with a digit.
◇An identifier cannot be a reserved word. (See Appendix A,
“Java Keywords,” for a list of reserved words).
◇An identifier cannot be true, false, or
null.
◇An identifier can be of any length.
Variable Names
• Valid Variable Names: These rules apply to all Java names, or
identifiers, including methods and class names
• Starts with: a letter (a-z or A-Z), dollar sign ($), or underscore (_)
• Followed by: zero or more letters, dollar signs, underscores, or
digits (0-9).
• Uppercase and lowercase are different (total ≠ Total ≠ TOTAL)
• Cannot be any of the reserved names. These are special names
(keywords) reserved for the compiler. Examples:

class, float, int, if, then, else, do, public, private, void, …
Good Variable Names
• Choosing Good Names àNot all valid variable names are good variable names
• Some guidelines:
• Do not use `$’ (it is reserved for special system names.)
• Avoid names that are identical other than differences in case (total, Total, and
TOTAL).
• Use meaningful names, but avoid excessive length
• crItm à Too short
• theCurrentItemBeingProcessed à Too long
• currentItem àJust right
• Camel case capitalization style
• In Java we use camel case
• Variables and methods start with lower case
• dataList2 myFavoriteMartian showMeTheMoney
• Classes start with uppercase
• String JOptionPane MyFavoriteClass
Valid/Invalid Identifiers
Valid:
$$_ Valid but not recommended. Not a descriptive variable
R2D2 Valid but not recommended. Not a descriptive variable
INT okay. “int” is reserved, but case is different here
_dogma_95_
riteOnThru
SchultzieVonWienerschnitzelIII Valid but too long
Invalid:
30DayAbs starts with a digit
2 starts with a digit
pork&beans `&’ is illegal
private reserved name
C-3PO `-’ is illegal
JAVA KEYWORDS

9
Primitive Data Types
• Java’s basic data types:
• Integer Types:
• byte 1 byte Range: -128 to +127
• short 2 bytes Range: roughly -32 thousand to +32 thousand
• int 4 bytes Range: roughly -2 billion to +2 billion
• long 8 bytes Range: Huge!
• Floating-Point Types (for real numbers)
• float 4 bytes Roughly 7 digits of precision
• double 8 bytes Roughly 15 digits of precision
• Other types:
• boolean 1 byte {true, false} (Used in logic expressions and conditions)
• char 2 bytes A single (Unicode) character
• String is not a primitive data type (they are objects)
Numeric Constants (Literals)
• Specifying constants: (also called literals) for primitive data types.
Integer Types:
byte
short optional sign and digits (0-9): 12 -1 +234 0 1234567
int
long Same as above, but followed by ‘L’ or ‘l’: -1394382953L

Floating-Point Types: Avoid this lowercase L. It looks


doubleTwo allowable forms: too much like the digit ‘1’
Decimal notation: 3.14159 -234.421 0.0042 -43.0
Scientific notation: (use E or e for base 10 exponent)
3.145E5 = 3.145 x 105 = 314500.0
1834.23e-6 = 1834.23 x 10-6 = 0.00183423
float Same as double, but followed by ‘f’ or ‘F’: 3.14159F -43.2f

Note: By default, integer constants are int, unless ‘L’/‘l’ is used to indicate
they are long. Floating constants are double, unless ‘F’/‘f’ is used to
indicate they are float.
Character and String Constants
• char constants: Single character enclosed in single quotes (‘…’) including:
• letters and digits: ‘A’, ‘B’, ‘C’, …, ‘a’, ‘b’, ‘c’, …, ‘0’, ‘1’, …, ‘9’
• punctuation symbols: ‘*’, ‘#’, ‘@’, ‘$’ (except single quote and backslash ‘\’)
• escape sequences: (see below)
• String constants: Zero or more characters enclosed in double quotes (“…”)
• (same as above, but may not include a double quote or backslash)
• Escape sequences: Allows us to include single/double quotes and other special
characters:
\” double quote \n new-line character (start a new line)
\’ single quote \t tab character
\\ backslash
• Examples: char x = ’\’’ ® (x contains a single quote)
”\”Hi there!\”” ® ”Hi there!”
”C:\\WINDOWS” ® C:\WINDOWS
System.out.println( ”Line 1\nLine 2” ) prints

Line 1
Line 2
Data Types and Variables
Study how Java checks the compatibility of the data types in
the expressions.

• int num1, num2; // num1 and num2 are integer variables


• double num3; // num3 is a double variable
• String word; // word is a string variable
• boolean val; // val is a boolean variable
• char symbol; // symbol is a character variable

• num1 = 7; // legal (assigns the value 7 to num1)


• val = true; // legal (assigns the value true to val)
• symbol = ‘#’; // legal (assigns character # to symbol)
• word = “cat” + “bert”; // legal (assigns the value “catbert” to word)
• num3 = num1 – 3; // legal (assigns the integer value 7 – 3 = 4 to double num3)

• val = 5; // illegal! (cannot assign int to boolean)


• num2 = num1 + val; // illegal! (cannot add int and boolean)
• symbol = num1; // illegal! (cannot assign int to char)

You might also like