0% found this document useful (0 votes)
62 views5 pages

Comprog Midterms Review

Primitive data types in Java include boolean, char, int, long, float, and double. Variables are used to store and reference values of these primitive data types. Constants cannot be changed once declared. Operators like arithmetic, relational, and logical are used to perform operations on primitive data values and variables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views5 pages

Comprog Midterms Review

Primitive data types in Java include boolean, char, int, long, float, and double. Variables are used to store and reference values of these primitive data types. Constants cannot be changed once declared. Operators like arithmetic, relational, and logical are used to perform operations on primitive data values and variables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PRIMITIVE DATA TYPES  boolean – only two possible values: true

or false. Example: boolean isNice = true;


DATA TYPE – values have particular data types.  char – to store single character. In java,
They are used to specify the set of values and it is enclosed with single quotes.
operations. Example: char letter = ‘A’;
PRIMITIVE DATA TYPE – fundamental data
CONSTANTS AND VARIABLES
types in java. These data types are basic data
types that represent simple values like VARIABLE - a name for a memory location that
numbers or characters. They are predefined stores a specific value. This is an identifier. Can
(already defined) in Java. hold one value at a time, but value may change
during program execution. Declaration:
8 BUILT-IN PRIMITIVE DATA TYPES
data_type variable_name = value;
8-bit Stores whole numbers from - int x = 10;
byte
integer 128 to 127
To declare more than one variable of the same
short
16-bit Stores whole numbers from - data type or specified type, use a comma-
integer 32,768 to 32,767 seperated list. Example:

32-bit Stores whole numbers from - I. int a, b, c;


int
integer 231 to 231-1 II. int a = 10, b = 3, c = 1 + 2;
III. char letter; letter = ‘A’;
64-bit Stores whole numbers from -
long CONSTANT – memory location whose value
integer 263 to 263-1
cannot be changed during program execution.
Stores fractional numbers.
To declare constant values, use ‘final’ reserved
float 32-bit Sufficient for storing 6 to 7
decimal digits keyword: final data_type variable_name =
Stores fractional numbers. value; Example: final double PI = 3.14159;
double 64-bit Sufficient for storing 15
decimal digits TYPE CASTING
1-bit of
boolean True/false
information Type Casting - refers to converting a value
‘\u0000’ (or 0) to ‘\uffff’ (or from a specific type to a variable of another
16-bit
65,535 inclusive) Stores a
char Unicode
single character/letter or ASCII type. Two types of convertion:
character
values
 Widening (implicit casting) - lower to
 byte - saving memory space in large
higher precision data type. No loss of
arrays. Example: byte b = 100;
information, casting will be preformed
 short - saving memory space in large
by the Java Virtual Machine (JVM)
arrays. Example: short s= 1400;
implicitly or automatically.
 int - generally used as the default data
type for integral values unless there is a
concern about memory space. Example:
int i = 12400; int x = 4;
double y = x;
 long - used when a wider range than int //value of y is 4.0 because it got
is needed. Example: long l = 124000L; converted to double
 float - save memory in large arrays of  Narrowing (explicit casting) – high to
floating-point numbers. lower precision data type. Typically
Example: float f1 = 234.5f; involve loss of information. The
 double – generally the default data type following is an example of Java’s strict
for decimal values. type checking:
Example: double d1 = 234.5d; (or float x = 4.1f;
without the ‘d’ after the value 234.5) int y = x;
//cannot be converted
It should be made explicit by the programmer RELATIONAL/COMPARISON OPERATORS -
through a cast operator: (data_type_name) used to evaluate the relation between the
expression. The following is an example of operands and generate a decision. These
explicit casting: returns Boolean value. Declarations: int x = 11,
float x = 4.1f; y = 3;
int y = (int) x;
Greater
//the value of y is 4. > x>y True
than
Greater
>= than or x >= y True
When using cast operator to convert decimal equal
number (float, double) into integer (int, short, < Less than x<y False
byte etc.), it will not round off the number. Less than
<= x <= y False
int a = (int) 7.9; //returns 7 or equal
double b = (double) 25; // returns 25.0 == Equal x == y False
float c = (float) (3 + 5); // returns 8.0
Not
!= x != y True
Equal
OPERATORS The relational expressions can also be used to
assign value in variables. For example:
Operators - These symbols tell the compiler or
interpreter of the program to perform a specific boolean b = 10 > 2; //result is true and
mathematical, relational, or logical operation. assigned in variable b. So, the variable b stored
the value true.
ARITHMETHIC OPERATORS - used to perform
basic mathematical operations. A value used on LOGICAL OPERATOR - return a Boolean value
either side of an operator is an operand. based on the Boolean result of the given
Example, in the expression 2 + 3, 2 and 3 are expressions. Always evaluated from left to
operands. Declarations: int x = 11, y = 3; right. Example:

+ Addition x+y 14 boolean a = 3 > 2; //store true value


- Subtraction x-y 8 boolean b = 2 < 1; //store false value
* Multiplication x*y 33
3 (not 3.66 AND operator:
because the Both operands
/ Division x/y && should be true a && b False
data types
are int) to make
2 condition true.
% Modulus x%y OR operator: at
(remainder)
The division and modulus have special least one
consideration. Java supports two types || operand is true a || b True
division: to make
condition true
 Floating point Division occurs when XOR operator:
one or both operands are floating point only one
or decimal (or the data types are float or ^ operand is true a^b True
double). Example: to make
int x = 11; double y = 3; //output is 3.66 condition true
 Integer Division occurs when both NOT operator:
it reverses !a False
operands are integers. The result will be
condition. If the
integer and if it has a remainder, it will
! condition is !(a && b) True
be removed. Example: true, the NOT
int x = 11; int y = 3; //output is 3 operator makes
a && !b True
it false.
ASSIGNMENT OPERATORS – used to assign ORDER OF PRECEDENCE - rules that specifies
values. Left operand gets the value of the which operations need to be performed in an
expression on the right. Declarations: int score; expression. Operation that needs to be perform
first.
Assigns values
from right side Highest Unary NOT !
= score = 32 32
operands to the
Multiply, Divide,
left side operands *, /, %
Modulo
Adds right score += 2
operand to the left Add, Subtract +, -
+= operand and Same as: 34
assigns the result score = score Intermediate Relational >, >=, <, <=
to the left operand +2
Equality ==, !=
Subtracts right score -= 3
operand to the left Logical AND &&
-= operand and Same as: 31
Logical OR ||
assigns the result score = score -
to the left operand 3
Lowest Assignment =
Multiply right score *= 2 In programming, when you have multiple math
operand to the left
Same as: operations, like adding and multiplying,
*= operand and 62
score = score parentheses are used to specify the order in
assigns the result
to the left operand *2 which these operations are done. For example:
Divide right score /= 2 We have some variables: a = 10, b = 3, and c = 8
operand to the left
/= operand and Same as: 31 (a + b) * c means first add a and b, then
assigns the result score = multiply by c, resulting in 104.
to the left operand score /2
a + (b * c) means first multiply b and c, then
It takes modulus score %= 2
using two (2) add the result to a, resulting in 34.
%= operands and Same as: 1
score = score The position of parentheses changes the order
assigns the result of calculations and affects the final answer. So,
to the left operand %2
parentheses are like traffic signs for the
Adds 1 to the left score++ computer, telling it which math to do first.
operand and Same as:
++ 2
assigns the result score = score
to the left operand +1 EXPRESSION – made up of variables/operators
Subtracts 1 to the score-- that evaluates a single value. Example: x = 12
left operand and and 11 + 13. Can also return other types of
-- Same as: 1
assigns the result values such as String or Boolean.
score = score - 1
to the left operand
In simple terms, calculate then assign. First, it ARITHMETIC EXPRESSION – returns numeric
calculates the given variable to the new value value. Consists of arithmetic operators,
depending on the operation (+, -, *, /, %). operands, and assignment operators. Two
types:
int score = 10; //given variable
 Integral expression – returns integer
score += 5; //new value
type of value, meaning operands are
integer. Example:
x = 11 + 3 * 24 – 5 / 4 //result: integer
type and value is 82
 Floating-point expression – operands INPUT & OUTPUT
are all in floating-point numbers,
returns floating-point value. Example: Scanner – a class in java.util package. Used to
y = 2.8 * 17.5 – 1.40 //result: floating- read data from standard input device and store
point value is 47.6 in variables.

MIXED EXPRESSION – an expression has To use, import the class:


different data type operands (contains integer import java.util.Scanner; or java.util.*;
and floating-point), returns floating-point
value. Example: Then, create an object of Scanner:

11.5 + 3 * 2.25 Scanner input = new Scanner(System.in);

Two rules to apply when evaluating mixed  “Scanner input” declares an object of
expression: Scanner type named “input”. Object
name can be any declared name by a
 Rule no. 1 - If the operator has the same programmer.
types of operands (either all integers or  “new Scanner(System.in)” creates a
all floating-point numbers), the operator Scanner object connected to the
is evaluated according to the type of the System.in property. System.in is an
operands. Example: inputStream which is connected to the
25 / 2 * 2.0 //evaluation: 12 * 2.0 = 24.0 standard input device (the keyboard).
25.0 / 2.0 * 2 //evaluation: 12.5 * 2 =
25.0 The Scanner class is used to get values from an
 Rule no. 2 - If the operator has both input device. Each value obtained is called a
types of operands (mixing integers and "token," which is a group of characters
floating-point numbers), during the separated by spaces.
calculation, the integer is temporarily Table 1 lists common methods of the Scanner
treated as a floating-point number. For class for reading various data types from the
example: keyboard. They fetch values you type, but if it's
25.0 / 2 * 2 //evaluation: 12.5 * 2 = 25.0 not the expected data type, they trigger an
LOGICAL EXPRESSION – these expressions error.
returns bolean value. Can consist of logical METHOD DESCRIPTION EXAMPLE
operators and relational expressions. For Retrieves
double x =
nextDouble() input as a
example: 14 >= 5 && 12 != 3 evaluates to true. objectName.nextDouble();
double
Retrieves int x =
nextInt()
If the expression has 2 or more logical input as an int objectName.nextInt();
operators, it is evaluated from left to right. Retrieves the
next line of
Example: 3 > 3 || 4 < 2 && 12 > 3 || 4 < 5 String x =
nextLine() data and
objectName.nextLine();
evaluates to true returns it as
a String
Parenthesis are used to group expression and Retrieves the
next complete String x =
control operators. Example: next()
token as a objectName.next();
String
(3 > 3 || 4 < 2) && (12 > 3 || 4 < 5) evaluates to Retrieves
short x =
false. nextShort() input as a
objectName.nextShort();
short
Retrieves byte x =
nextByte()
input as a byte objectName.nextByte();
Retrieves float x =
nextFloat()
input as a float objectName.nextFloat();
Retrieves long x =
nextLong()
input as a long objectName.nextLong();
The Scanner class doesn't have a "next" System.out.printf(“Price using print formatting:
method for characters. To get a single %.2f”, price); //result: Price using printf
character from the keyboard, use "next()" to formatting: 19.50
get a String, then access the first character with
With "printf," you format a double with 2
"charAt(0)." For example,
decimal places. "%.2f" is a format specifier,
"input.next().charAt(0)" gets the first character
like a placeholder for the number. "%.2f" says
of what you typed.
to show the number with 2 digits after the
char x = s.next().charAt(0); //gets the decimal point, and the "f" means it's a floating-
first character
point number.
The "next()" function gives the next word as a
string, and "charAt(0)" returns the first FORMAT
TYPE OF
character of that word. The "0" in "charAt(0)" is SPECIFIE EXAMPLE
OUTPUT
the position of the character within the word, R
and you can store it in a char variable. Single character: %c
Note: Sa math natin meron tayong nth term sa sequence %c Character A single character in
of numbers. Yung nth term natin is tinatawag na index sa a field of two (2)
programming. Yung first term or first index natin sa spaces: %2c
programming is ‘0’, kasi sa programming, nag start lagi sa Decimal
‘0’. Example: sa Input na “BSIT”, yung index 0 natin is %d integer An integer
letter ‘B’, then index 1 is ‘S’. number
Output Statements – used to send output to A floating-point
the standard output device, the display screen. number: %f
Example: System.out.print(“Hello”); and With 2 digits after
Floating-
the decimal point:
System.out.println(“Hello”); %f point
%.2f
number
 print() method shows output on the With 2 digits after
same line. the decimal in a field
 println() moves to the next line after of 6 spaces: %6.2f
displaying the output. Exponential A floating-point
floating- number in
%e
Concatenation - joining or combining pieces of point exponential format:
text (strings) to create a longer piece of text. number %e
A string formatted to
When displaying a string with variables, simply a field of 10 spaces:
concatenate them using the plus (+) sign. For %10s
example: %s String
With first 2
characters of the
String name = “Jess Diaz”;
string: %.2s
System.out.println(“Hello! My name is ” + name
+ “.”);
printf() method lets you format your output,
like controlling decimal places. It's like
customizing how a number looks when you
print it.
double price = 19.5;
System.out.println(“Price using println: ” +
price); // result: Price using println: 19.5

You might also like