0% found this document useful (0 votes)
2 views18 pages

Lecture 2

The document provides an overview of data types in Java, categorizing them into primitive (boolean, char, byte, short, int, long, float, double) and non-primitive types (classes, interfaces, arrays). It explains the characteristics and uses of each primitive data type, as well as the concept of variables, constants, and operators in Java programming. Additionally, it includes examples and exercises for better understanding of the material.

Uploaded by

atangsabel22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

Lecture 2

The document provides an overview of data types in Java, categorizing them into primitive (boolean, char, byte, short, int, long, float, double) and non-primitive types (classes, interfaces, arrays). It explains the characteristics and uses of each primitive data type, as well as the concept of variables, constants, and operators in Java programming. Additionally, it includes examples and exercises for better understanding of the material.

Uploaded by

atangsabel22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Data Types

What are DataTypes?


A data type represents the set of values that a variable may accept or
assume during program execution.
They are important because they indicate:
• The permissible operations for a variable
• The amount of memory allocated to it
• The range of values a variable can take
Data Types in Java

Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java:
1 Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
2 Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.

Primitive Data Types


There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and named by a keyword. Let us now
look into the eight primitive data types in detail.
Byte
• Byte data type is an 8-bit signed two's complement integer
• Minimum value is -128 (-2^7)
• Maximum value is 127 (inclusive)(2^7 -1)
• Default value is 0
• Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an integer.
• Example: byte a = 100,

Short
• Short data type is a 16-bit signed two's complement integer
• Minimum value is -32,768 (-2^15)
• Maximum value is 32,767 (inclusive) (2^15 -1)
• Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an integer
• Default value is 0.
int
• Int data type is a 32-bit signed two's complement integer.
• Minimum value is - 2,147,483,648 (-2^31)
• Maximum value is 2,147,483,647(inclusive) (2^31 -1)
• Integer is generally used as the default data type for integral values unless there is a concern about memory.
• The default value is 0
• Example: int a = 100000, int b = -200000

Long
• Long data type is a 64-bit signed two's complement integer
• Minimum value is -9,223,372,036,854,775,808(-2^63)
• Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
• This type is used when a wider range than int is needed
• Default value is 0L
• Example: long a = 100000L, long b = -200000L
float
• Float data type is a single-precision 32-bit IEEE 754 floating point
• Float is mainly used to save memory in large arrays of floating point numbers
• Default value is 0.0f
• Float data type is never used for precise values such as currency
• Example: float f1 = 234.5f
double
• double data type is a double-precision 64-bit IEEE 754 floating point
• This data type is generally used as the default data type for decimal values, generally the default choice
• Double data type should never be used for precise values such as currency
• Default value is 0.0d
• Example: double d1 = 123.4

boolean
• boolean data type represents one bit of information
• There are only two possible values: true and false
• This data type is used for simple flags that track true/false conditions
• Default value is false
• Example: boolean one = true

char
• char data type is a single 16-bit Unicode character
• Minimum value is '\u0000' (or 0)
• Maximum value is '\uffff' (or 65,535 inclusive)
• Char data type is used to store any character
• Example: char letterA = 'A'
Primitive Data Types
• Integers (int) e.g. -1, 0, 3, 1000, -2179, +123546.
• Floating point values (real numbers) (float, double) e.g. 98.6, 3.1419, -
3.4561e-5, 3, .4

• Characters (char) e.g. ‘A’, ‘1’, ‘%’, ‘?’, ‘/’, ‘}’


• Boolean (boolean) e.g. TRUE, FALSE
Abstract data types
• Predefined abstract data types includes:
• Arrays
• Strings
• Streams
• A user defined abstract data type is:
• Classes
Variables in Java
• A variable is a way of referring to a memory location used in a
computer program. A variable is identified by a name (identifier), and
it contains information/data that can change.

• Variables can be referred to as containers which carry data which can


change.
Declaration of Variables

• A variable declaration instructs the compiler to set aside memory for a


variable. The declaration states the type of data to be stored in this
memory location as well as the name used to refer to this data. A
declaration must be made before a variable can be used. Below is the
syntax for declaring variables.
• DataType identifier, identifier …;
Here are examples of variable declaration:
• int change; // variable name is “change” and it is an integer
• int pound, pence; // variable names, “pound” and “pence”, both integers
• float miles, kms; // variable names “miles” and “kms” both real numbers
• string firstName; // variable name “firstName” of data type string
Constants in Java

• Constants do not change their value during program execution. There


are two types: Literal constants and named constants.
Constants

Literals e.g 1, Named constants e.g. final


0.002, “today” int pi = 3.142
.

Special Characters
These are characters which have a function they perform
Special Character Function

\b Back space

\f Form feed

\r Carriage return

\n New line

\t Tab
Initialization of Variables

• Initializing a variable means assigning a value to the variable for the first
time. A variable can be initialized when it is declared or after it has been
declared. Below is the syntax for the two possible ways to initialize a
variable.
• Data_type Identifier = Data_value;
• Data_type Identifier;
• Identifier = Data_value;
• Examples of variable declaration are as follows:
• int grade = 91; //91 is stored
• float price = 2; // 2.0 is stored
Operators in Java

• Operators are symbols which takes one or more operands or


expressions and perform arithmetic or logical computations. A
consideration would be given to the arithmetic, assignment and
logical/relational operators.
Operators

Relational
Arithmetic e.g. +, -, /, Logic e.g. &&, ||,! e.g. >,<,==,!=
*, ++ , --, %
++ AND-- (UNARY INCREMENT AND DECREMENT OPERATORS)

. The operators ++ and -- are unary operators; they work with a single operand. They’re used to increment or decrement the
value of a variable by 1.
. Unary operators can also be used in prefix and postfix notation.
. In prefix notation, the operator appears before its operand:
int a = 10;
++a;
. In postfix notation, the operator appears after its operand:
int a = 10;
a++;
. When these operators are not part of an expression, the postfix and prefix notations
behave in exactly the same manner:
int a = 20;
int b = 10;
++a;
b++;
System.out.println(a);//prints 21
System.out.println(b);//prints 11
When a unary operator is used in an expression, its placement with respect to its operand decides whether its value
will increment or decrement before the evaluation of the expression or after the evaluation of the expression. See
the following code, where the operator ++ is used in prefix notation:
int a = 20;
int b = 10;
int c = a - ++b;
System.out.println(c); //prints 9
System.out.println(b);// prints 11

When ++ is used in postfix notation with an operand, its value increments after it
has been used in the expression:

int a = 20;
int b = 10;
int c = a - b++;
System.out.println(c); // print 10
System.out.println(b); // print 11

int a = 10;
a = a++ + a + a-- - a-- + ++a;// a = 10 + 11 + 11 - 10 + 10;
System.out.println(a);
The output of this code is 32.
Conditional Ternary Operator

• The conditional ternary operator evaluates an expression returning a


value if that expression is true and a different one if the expression is
evaluated as false. Its format is:
• Condition ? result1 : result2;
• If condition is true the expression will return result1, if it is not it will
return result2.
• 7 == 5 ? 4 : 3; //returns 3, since 7 is not equal to 5.
• 7 == (5 + 2) ? 4 : 3; //returns 4, since 7 is equal to 5 + 2.
• 5 > 3 ? a : b; //returns the value of a, since 5 is greater than 3.
• a > b ? a : b; //returns whichever is greater, a or b.
Questions?
Exercises
• Give valid examples for each of the basic data types.
• Write the following mathematical expressions in java using
appropriate symbols
• 10 mod 3
• (5b +2 )6x
• (NOT(FALSE OR TRUE) AND TRUE
• (5 > 7) OR (16.4 = 7 x 3)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy