Classes and Objects Flashcards
Scanner Class
Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double etc. and strings. It is the easiest way to read input in a Java program, though not very efficient if you want an input method for scenarios where time is a constraint like in competitive programming.
Package
A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer.
Steps in creating Scanner Class
Import:
import java.util.Scanner
Create Scanner: Scanner input = new Scanner(System.in);
Prompt:
System.out.println(“Please enter your name:”)
Keyboard Input:
String firstName = input.next();
Increment and Decrement Operators
Increment (++) and decrement (—) operators in Java programming let you easily add 1 to, or subtract 1 from, a variable.
++var
i = 1
int j = ++i
Increment var by 1, and use the new var value in the statement
int j = ++1; // j is 2 & i is 2
var++
i = 1
int j = i++
Increment var by 1, and use the original var value in the statement
int j = 1++; // j is 1 & i is 2
–var
i = 1
int j = –i
Decrement var by 1, and use the new var value in the statement
int j = --1; // j is 0 & i is 0
var–
i = 1
int j = i–
Decrement var by 1, and use the original var value in the statement
int j = 1--; // j is 1 & i is 0
Type Casting
Type Casting in Java. Type casting is used to convert an object or variable of one type into another.
Implicit Casting
An implicit cast means you don’t have to write code for the cast.
An implicit cast happens when you’re doing a widening conversion.
Explicit Casting
An explicit casting is where you use some syntax to tell the program to do a conversion.
Shorthand Assignment Operators
In addition to the basic assignment operator, Java also defines 12 shorthand assignment operators that combine assignment with the 5 arithmetic operators ( += , -= , *= , /= , %= ) and the 6 bitwise and shift operators ( &= , |= , ^= , «= ,»_space;= ,»_space;>= ).
Comparison Operators
Operators The result of every comparison is boolean (true or false). operator meaning < less than <= less than or equal to == equal to >= greater than or equal to > greater than != not equal