Lecture 2 - Java Basics Flashcards
What is a variable ?
A variable refers to a named storage location, that stores a
value of a particular data type.
A variable has a name (aka identifier), a type, and stores a
value of the declared type.
What is the difference between declaring a variable in python and declaring one in java ?
In Java every variable must
be declared as one of the data types before using it.
What is a constant ?
It refers to a data item which cannot be changed.
The keyword final must be used in the declaration of a constant. E.g., final int HOURS IN TOTAL=150;
Naming conventions for constants ans variables :
Variables: mixed case, lower case first letter E.g., age, buttonColor, dateDue
Constants: all upper case, words separated by underscore. E.g., PI, MIN VALUE, MAX VALUE
What primitive datatypes are there in java?
boolean char byte short int long float double
What is the suffix L used for ?
d to indicate it is of type long (e.g.,
234L, OX3E3l). The uppercase L is preferred.
int a=012 what is a ?
Because, it’s taken as octal base (8), since that numeral has 0 in leading it’s corresponding decimal value is 10
byte d=130;
error because byte accepts values smaller than 128
float g = 1.02f;
correct remember to add the small f
char h = “B”;
error it needs single brackets ‘B’
boolean j = TRUE;
error it needs to be all in small letters
What is casting ?
To convert one numeral type to another numeral type ( primitive conversion).
What are the two types of casting ?
- Widening (Implicit) : short a=20; long b=a; int c=20; float d=c;
- Narrowing (Explicitly done): long a=20; short b=(short)a; float c=20; int d=(int)c; float hight=(float)0.158;
What is overflow?
When an operation with integer types may produce numbers which are too big to be stored in the primitive types you have allocated.