Obj 1: Handling date, time, text, numeric and boolean values Flashcards
Java primitive type
Java has eight built- in data types.
A primitive is not an object in Java,
nor does it represent an object. A primitive is just a single value in memory, such as a number
or character.
What are the primitive types
boolean, byte, short, int, long, float, double, char
default values of:
- boolean
- byte
- short
- int
- long
- float
- double
- char
- false
- 0
- 0
- 0
- 0L
- 0.0f
- 0.0
- \u0000 (nil)
Does it compile
float x = 1.0 ;
No, a float requires the letter f or F following the number so Java knows it is a float. Without an f or F, Java interprets a decimal value as a double
Does it compile
long x = 123000;
No, a long requires the letter l or L following the number so Java knows it is a long.
Without an l or L, Java interprets a number without a decimal point as an int in most
scenarios.
short s = -123;
char c = (char) s;
Does it compile ?
Yes, short and char values can be cast to one another because the underlying data size is the same
A number in the code is called …
literal
long max = 3123456789;
Does it compile ?
No, Java complains the number is out of range.
The solution is to add the character L to the number:
long max = 3123456789L; // Now Java knows it is a long
Does Java allow other decimal number system.
Yes, Java allows you to
specify digits in several other formats:
* Octal (digits 0–7), which uses the number 0 as a prefix— for example, 017.
* Hexadecimal (digits 0–9 and letters A–F/a–f), which uses 0x or 0X as a prefix— for
example, 0xFF, 0xff, 0XFf. Hexadecimal is case insensitive, so all of these examples
mean the same value.
* Binary (digits 0–1), which uses the number 0 followed by b or B as a prefix— for
example, 0b10, 0B10.
double annoyingButLegal = 1_00_0.0_0;
does it compile ?
Yes, You can add underscores anywhere except at the beginning of a literal, the end of a literal, right before a decimal point, or right after a decimal point. You can even place multiple underscore characters next to each other.
Each primitive type has a wrapper class, which is an object type that corresponds to the primitive.
- boolean
- byte
- short
- int
- long
- float
- double
- char
- Boolean
- Byte
- Short
- Integer
- Long
- Float
- Double
- Character
Only Boolean and Character do not extend the class Number
int x = 123;
Integer y = Integer.valueOf(x);
String str = “123”;
Integer wrapper = Integer.valueOf(str);
int primitive = Integer.parseInt(str);
does code compile ?
Yes
There is also a valueOf() variant that converts a String into the wrapper class
String str = “””
Hello \s
World!”””;
String str = “””
Hello \
World!”””;
String str = “””
Hello \n
World!”””;
String str = “””
Hello \s World!”””;
String str = “””
Hello \nWorld!”””;
Hello
World! (2 lines)
Hello World! (1 line)
Hello
World! (3 lines)
Hello World! (1 line)
Hello
World! (2 lines)
int value = 3; // Stored as 0011 int complement = ~value; // Stored as 1100 System.out.println(value); // 3 System.out.println(complement); // - 4
how to find the bitwise complement of a number ?
multiply it by negative one and then subtract one.
```
System.out.println(-1value - 1); // - 4
System.out.println(-1complement - 1); // 3
~~~
int pelican = !5; boolean penguin = - true; boolean peacock = !0;
does it compile ?
No, you cannot apply a negation operator (- ) to a boolean expression, nor can you apply a logical complement operator (!) to a numeric expression.