Lecture2b-variables-Identifiers-data-types-assignments Flashcards
What is an identifier in Java?
A name used for variables, classes, or methods.
What are reserved words in Java?
Special words (e.g., int
, double
, class
) that cannot be used as variable names.
What are the three main parts of a variable?
- Name (Identifier)
-
Data Type (e.g.,
int
,double
) - Value (Stored data)
Example: int count = 1;
What is the difference between local and global variables?
✔ Local Variable: Exists only inside the method where it’s declared.
✔ Global Variable: Exists throughout the entire program.
What are Java’s 8 primitive data types?
- byte
- short
- int
- long
- float
- double
- char
- boolean
What are Java’s integer data types?
✔ byte: -128 to 127
✔ short: -32,768 to 32,767
✔ int: -2,147,483,648 to 2,147,483,647
✔ long: Very large numbers
What are Java’s floating-point data types?
✔ float – Stores decimal numbers with 7 digits of precision.
✔ double – Stores decimal numbers with 15 digits of precision.
What values can a boolean hold?
true or false
Example:
```java
boolean isJavaFun = true;
~~~
How do you declare a char
in Java?
java char letter = 'A';
✔ Uses single quotes (‘ ‘)
✔ Stores one character
How do you declare a String
in Java?
java String name = "Java";
✔ Strings use double quotes (“ “)
✔ Strings are objects, not primitive types.
What does the +
operator do with strings?
Joins (concatenates) strings.
Example:
java System.out.println("Hello " + "World!");
🔹 Output:
Hello World!
How do you declare a constant?
Use final
to prevent changes.
java final int MAX_SPEED = 120;
✔ Constants use ALL UPPERCASE
What is an assignment statement in Java?
Stores a value in a variable using =
.
Example:
java int x = 5;
🔹
x
is assigned the value 5.How does Java handle division and remainder operations?
✔ Integer division (/
) drops decimals (e.g., 7 / 2 = 3
)
✔ Modulus (%
) gives the remainder (e.g., 7 % 2 = 1
)
How do you calculate exponents in Java?
Use Math.pow(base, exponent)
.
Example:
```java
System.out.println(Math.pow(2, 3)); // Output: 8.0
~~~
What are ++
and --
used for?
✔ x++
→ Adds 1 (x = x + 1
)
✔ x--
→ Subtracts 1 (x = x - 1
)
What are Java’s compound operators?
✔ +=
(Addition assignment)
✔ -=
(Subtraction assignment)
✔ *=
(Multiplication assignment)
✔ /=
(Division assignment)
✔ %=
(Modulus assignment)
Example:
```java
x += 5; // Equivalent to x = x + 5;
~~~
What are the two types of data conversion?
-
Widening Conversion (Safe) – Converts small to large (e.g.,
int → double
). -
Narrowing Conversion (Unsafe) – Converts large to small (e.g.,
double → int
).
Example of casting:
```java
double num = 10.5;
int x = (int) num; // x = 10
~~~
What are the three types of data conversion in Java?
-
Assignment Conversion: Assigning a smaller type to a larger one automatically (e.g.,
int → double
). -
Promotion: Automatic conversion in expressions (e.g.,
int + double = double
). -
Casting: Manually converting types using
(type)
(e.g.,(int) 3.5
→3
).
What is JShell in Java?
A Read-Evaluate-Print Loop (REPL) that allows testing Java code interactively.
Example usage:
```sh
jshell> int x = 5;
x ==> 5
jshell> x + 3;
$2 ==> 8
jshell> /exit
~~~
What is casting in Java, and how is it used?
Casting is manually converting one data type into another using (type)
.
What is Widening Cast in Java?
Widening Cast is a safe, automatic conversion from a small to a large type.
Example:
```java
int num = 10;
double d = num; // Implicit casting (int to double)
~~~
What is Narrowing Cast in Java?
Narrowing Cast is an explicit, risky conversion from a large to a small type.
Example:
```java
double d = 10.99;
int num = (int) d; // Explicit casting (double to int), drops decimals
~~~
When is casting used in Java?
Casting is used when Java does not automatically convert types.
What are numeric literals in Java?
A numeric literal is a constant value written directly in code.
What is an integer literal?
An integer literal is a whole number written directly in code.
Example: int years = 34; // 34 is an integer literal
What is a floating-point literal?
A floating-point literal is a number that has a decimal point written directly in code.
Example: double pi = 3.14159; // 3.14159 is a floating-point literal
What is a caution regarding integer literals?
Integer literals must fit within the variable’s type (byte, short, int, long).