Assignments Flashcards Preview

JAVA OCA7 > Assignments > Flashcards

Flashcards in Assignments Deck (75)
Loading flashcards...

What is a literal?

A primitive literal is merely a source code representation of the primitive data
types—in other words, an integer, floating-point number, boolean, or character that
you type in while writing code. The following are examples of primitive literals:
'b' //char literal
42 //int literal
false //boolean literal
2546789.343 //double literal


What are the ways to represent integers in Java?

There are 4 ways: decimal (base
10), octal (base 8), hexadecimal (base 16), and as of Java 7, binary (base 2).


What are the rules for numeric literals with underscores and when was it introduces?

It was introduced by Java7 and it can not be used at the beginning of the literal or at the end or just after decimal point. It can be used by any of the numeric types including doubles and floats.
int pre7 = 1000000; // pre Java 7 – we hope it's a million
int with7 = 1_000_000; // much clearer!
int i1 = _1_000_000; // illegal, can't begin with an "_"
int i2 = 10_0000_0; // legal, but confusing


How do we write binary literals?

By putting a 0B or 0b to the beginning:
int b1 = 0B101010;
int b2 = 0b00011;


How do we write octal literals?

They start with 0. They can have up to 21 digits not including the leading 0.
int six = 06; // Equal to decimal 6
int seven = 07; // Equal to decimal 7
int eight = 010; // Equal to decimal 8
int nine = 011; // Equal to decimal 9


How do we write hexadecimal literals?

They start with 0x or 0X and can have up to 16 digits not including the prefix.
int x = 0X0001;
int y = 0x7fffffff;
int z = 0xDeadCafe;


Can binary, octal, decimal, and hexadecimal integer literals can be in other types and how?

They can be specified as long by placing a suffix at the end like:
long jo = 110599L;
long so = 0xFFFFl; // Note the lowercase 'l'


How do we define floating point literals?

For double no need to put an explicit D or d to the end but for float put F or f because it is a way to tell compiler that you are aware of what you are doing when you assign the number to a less precise container.


How do we define boolean literals?

Only as true or false. Numbers not accepted as in some languages.


How do we define char literals?

Inside single quotes but can be defined as unicode as well or any number literal that fits 16-bit unsigned range. We can also escape characters.
char a = 'a';
char b = '@';

char letterN = '\u004E'; // The letter 'N'

char a = 0x892; //hexadecimal literal
char b = 982; //int literal
char c = (char)70000; // The cast is required; 70000 is out of char range
char d = (char) -98; //Ridiculous,but legal

char e = -29; // Possible loss of precision; needs a cast
char f = 70000; // Possible loss of precision; needs a cast
char c = '\"'; // A double quote
char d = '\n'; // A newline
char tab = '\t'; // A tab


What are the non-primitive literals?

Strings and arrays.


How does a reference variable hold values?

We don't know what the format
is. The way in which object references are stored is virtual-machine specific (it's a
pointer to something, we just don't know what that something really is). All we can
say for sure is that the variable's value is not the object, but rather a value representing a specific object on the heap.


What happens if you assign different types of literals to each other?

byte b = 27;
27 is implicitly an int. But that assignment is valid only because the compiler automatically narrows the literal value to a byte .byte b = (byte) 27; // Explicitly cast the int literal to a byte


What is the result of an expression involving anything int -sized or smaller?

The result is always an int.In other
words, add two byte s together and you'll get an int —even if those two byte s are
tiny. Multiply an int and a short and you'll get an int . Divide a short by a byte
and you'll int.
byte a = 3; // No problem, 3 fits in a byte
byte b = 8; // No problem, 8 fits in a byte
byte c = a + b; // Should be no problem, sum of the two bytes fits in a byte
Buttt it won't compile: possible loss of precision found: int
required: byte
byte c = a + b;
It would have compiled if we'd done the explicit cast:
byte c = (byte) (a + b);


Is this expression legal?
int j, k=1, l, m=k+3;

Yes, k is initialized before m uses it


Is this expression legal?
int j, k=m+3, l, m=1;

No, m is not initialized before k uses it


Is this expression legal?
int x, y=x+1, z;

No, x is not initialized before y uses it


What are types of casts?

There are two. Can be ımplıcıt or explıcıt.


What is an implicit cast?

You do not specify anything in code. That happens when you put somethıng smaller to a bigger container. It is a widening conversion


What is an explicit cast?

When you put a bigger value to a smaller container-narrowing- you get a compile error saying 'possible loss of precision'. But if you explicitly specify that you re aware of the loss and add type to code, that's explicit conversion.


double d = 100L; Does that give an error?

No double can hold every information long can store. So that's an implicit conversion.


What happens when you cast a floating number to an integer?

It looses all the digits after decimal.


class Casting {

public static void main(String [] args) {

long l = 130L;

byte b = (byte)l;

System.out.println("The byte is " + b);



%java Casting

The byte is -126

The leftmost bit is 1 so it's negative.


What are he properties of boolean data type?

Size in bytes: --
Internal representation: Not precisely defined
Range: true or false


What are he properties of byte data type?

Size in bytes: 1
Internal representation: 8-bit two's complement
Range: −128 to +127


What are he properties of char data type?

Size in bytes: 2
Internal representation: Unicode
Range: \u0000 to \uffff


What are he properties of short data type?

Size in bytes: 2
Internal representation: 16-bit two's complement
Range: –32768 to 32767


What are he properties of int data type?

Size in bytes: 4
Internal representation: 32-bit two's complement
Range: −2,147,483,648 to 2,147,483,647


What are he properties of long data type?

Size in bytes: 8
Internal representation: 64-bit two's complement
Range:-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807


What are he properties of float data type?

Size in bytes: 4
Internal representation: 32-bit IEEE 754
floating point
Range: 3.4e +/- 38 (7 digits)