Basic java Flashcards
Do you need to import Java.lang library in a Java program
No, it is available by default
Can you have the class name of a java program different from the name of the .java file it resides in?
Yes but not in case of public classes, The file name and public class name should match else you will get a compile time error saying “class <> is public, should be declared in a file named <>.java”
How to escape a character like double quote, backslash etc.?
By putting a backslash() in front of the character to be escaped.
Scanner class
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods
should an IO class like Scanner be closed
Yes, this should be done to avoid memory leakage and can be done by calling close() method on the scanner class object
variables - private; methods - public
variables in a class should be declared as private to restrict access to them and allow access only through get and set methods. Hence methods should be declared public.
Primitive Types
boolean(True/false), byte(one byte), char(2 bytes), short, int, long, float, double
Reference types
Anything which does not belong to the primitive types is of reference type. Reference types start with a capital letter
Does reference type store the actual object
no, they store a reference to an object not the actual value of the object
constructor features
have the exact same name as the class; do not have a return type; a class can have as many constructors as needed but they should differ in parameters and/or types; once a constructor is defined the compiler won’t provide a default one, all versions of the constructors needed will have to be defined by the user
approximation in double type
For financial calculations avoid using double variables as double approximates values. Use third party libraries of bigdecimal insterad
What is input stream?
An object from which we can read a sequence of bytes is called an input stream
What is an output steam?
An object to which we can write a sequence of bytes is called an output stream
JDK
Jave development kit - Includes JRE - Needed for development in Java
JRE
Java runtime environment - needed to run Java applications
Line Comments
//
Block comments
/* … */
Javadoc comments
/** … */
line comments in a block comment
valid and no compile time error
Package naming conventions
Should be all lower case
use reverse domain name to insure uniqueness for example pluralsight.com will use com.pluralsight
Add further qualifiers to ensure uniqueness within the company/group
allowed characters for variable names
letters, numbers, dollar sign and underscore
Convention for variables names
only letters and numbers are used conventionally. Camel case is followed for casing
Four integer types with sizes
Byte - 8 bits
Short - 16 bits
Int - 32 bits
Long - 64 bits
The result of divide operation on floating point operands and integer operands
example 13.0/5.0 will result in 2.6 whereas 13/5 will give 2
The result of divide operation on floating point operands and integer operands
example 13.0/5.0 will result in 2.6 whereas 13/5 will give 2
Operator precedence
postfix then prefix then multiplicative then additive.
How are operators of equal precedence evaluated
left to right
Implicit type conversion rule for mixed integer types in an equation
Converted to largest integer type in the equation
Implicit conversion types for mixed floating types in an equation
Converted to double as double is the largest floating point type
Implicit conversion types for mixed integer and floating types in an equation
Converted to the largest floating point type in the equation
Conditional assignment
result = condition?true-value:false-value
Variables’ scope
Variables that are in scope when the block start remain in scope inside the block
Variables declared within a block are out of scope outside the blocks
Conditional logical operators
the expression at the right side of the operator is evaluated only if it is needed to determine the final result
Encapsulation
The concept of hiding internal representation of an object
visibility with no access modifiers
Also called package private. Visible only within its own package. Usable on classes and members of a class
Private access modifier
Visible only within its own class. Not usable on top level classes but only members of a class
Naming convention for classes
Should use ‘Pascal Case’. The name should be a descriptive noun without any acronyms
THIS keyword
THIS is an implicit reference to the current object
What does null represent
It represents an uncreated object
Mechanisms to establish initial states of an object
Field initializers
constructors
Initialization blocks