Basic java Flashcards
(101 cards)
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