Basics of Java Flashcards
What is Java?
a programming language that allows you to provide instructions to a computer, and with which you can create computer programs
Java compiler
translates Java code into machine or computer language so the computer can execute the program
How to delineate a single line and multi line comment?
single line comment //
multi line comment /…/
How to name the program?
class Main {…}
tells Java the name of the program is Main
What is the main method?
static public void main (String[] args)
this statement is called the main method and should be included in every Java program. Tells the compiler this is the beginning of the Java program. ‘public’ and ‘static’ can be interchanged.
Is syntax important in Java?
proper syntax is essential or the program will not run.
What is an executable statement?
code that produces output
statements in Java must end with a semicolon;
What does public mean in
static public void main (String[] args)
means that any object can use the main method
What does static mean in
static public void main (String[] args)
means that the main method is a class method, also means you can access this method without having an instance of the class
What does void mean in
static public void main (String[] args)
tells Java the main method won’t return a value. Other methods in other classes can receive and return values/variables, but main cannot return anything
What does main mean in
static public void main (String[] args)
the main method is responsible for calling any other methods within the program. This is the first thing the compiler looks for in the program.
What does string args in
static public void main (String[] args)
sets up the main method to accept parameters, once packaged and distributed users can specify arguments to pass to the main function
What is one argument the main method takes?
an array of string elements
(String[] args)
What does args do in
static public void main (String[] args)
holds the actual parameters passed in
What are java statements?
instructions that tell the programming language what to do
What is an assignment statement?
assigns a value to a variable
What is a Java method?
blocks of code that perform a task and can be used by other parts of a computer program such as a declaration statement.
What are the core Java statements that end in curly brackets instead or semicolon?
if statement
while statement
for loop statement
What is the difference between rules and conventions in Java?
Rules are hard and fast: your program won’t compile or run if you violate the rule. Conventions are practices that most Java programmers follow.
What are keywords?
words that have a predefined meaning for the language
Rules of naming variables:
- variables are case sensitive
- cannot be Java keywords
- main/Main is not a keyword in Java
- no spaces in variable names
Naming conventions for constants:
- convention: use uppercase with underscore
final FIRST_NAME = “Sandy”; - rule: constant names cannot be Java keywords
- rule: constants cannot have spaces
Naming convention for classes and interfaces:
Capitalized nouns, each word capitalized
Employee, UnionEmployee, Remote
Naming convention for methods:
lowercase verbs
public void enterData() {}
Naming conventions for variables:
short and meaningful
int[] counter, double fees;
Naming conventions for constants:
all uppercase
universe = ALL;
Naming conventions for packages:
all lowercase letters
java.util.*;
Describe the short data type:
smallest type at only two bytes (16 bits)
It accepts values from -32,768 to 32,767
it’s signed, meaning it accepts both negative and positive values
short is used more for lower-level programming, such as image processing or working with sound processing
Describe the int data type:
consumes 32 bits in memory, is a whole number
the valid number range is:
-2,147,483,648 to 2,147,483,647
Describe the long data type:
64 bits of memory and accepts a range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
It’s useful for storing numbers that outgrow the integer data type.
Describe float data type:
A float data type is a single precision number format that occupies 4 bytes or 32 bits in computer memory
A float data type in Java stores a decimal value with 6-7 total digits of precision. So, for example, 12.12345 can be saved as a float, but 12.123456789 can’t be saved as a float
default value is 0.0f
Describe double data type:
A double data type is a double precision number format that occupies 8 bytes or 64 bits in the memory of a computer.
The double data type stores decimal values with 15-16 digits of precision
default value is 0.0d
Describe the char data type:
char is short for character, 16 bits in size, single character, can be specified via unicode ie. 7 7=M or with single quotes ‘M’
What is unicode?
Unicode is a computer encoding methodology that assigns a unique number for every character.
65=A
66=B
67=C…..
Describe the Java string data type:
a sequence of single characters, a string is also a class, which means that it has its own methods for working on strings.
Because a string is a class, when we declare a string, we are creating an instance of that class. Therefore, the keyword new is used to create a new instance
length()
outputs the length of the string in number of characters
toUpperCase()
toLowerCase()
output string to all upper case or lower case
trim()
output removes all spaces in the string
replace()
replace all instances of a certain character sequence with another
Java escape codes
" double quote
\ backslash
\t tab
\n new line
Concatenation
adding strings together
String hello = new String(“Hello World!”);
String niceDay = new String(“It’s a nice day!”);
String output = hello + “ “ + niceDay;
output= Hello World! It’s a nice day!
Describe Java boolean data type:
a primitive data type, true or false, defaults to false
What are the Java primitive data types in order from smallest to largest?
1 bit - boolean
8 bit - byte
16 bit - char, short
32 bit - int, float
64 bit - long, double
What are the two type of data conversion?
Implicit conversion- moving a number from a smaller data type into a larger data type (one with more bits of storage), no information will be lost.
byte->short->int->long->float->double
‘char’ and ‘boolean’ data type are not eligible for implicit conversion
Explicit conversion- when choice does not allow, one must move one data type to another type of narrower size(one with fewer bits of storage). Truncation may be necessary. An error will be cast.
A type cast must be used to force Java to move the value explicitly.
What is a type cast?
A wider data type can be explicitly cast to a narrower type by enclosing the narrower data type name in parentheses and placing it in front of the variable declared with the wider data type.
Describe byte data type:
8-bit signed Java primitive integer data type. Its range is -128 to 127 (-27 to 27 - 1).
byte type is the smallest integer data type available in Java.
In what order does Java perform math operations?
The same order of operations as math does
PEMDAS
Java math operators:
+ addition
- subtraction
* multiplication
/ division
% modulo (returns the remainder of the division between two numbers)
What are relational operators?
They help us check if a condition is met by answering questions.
==
checking to see if the two values are equal to each other
careful!!! not using these correctly i.e. = instead of == will result in an assignment error and actually assign a value instead of comparing a value.
!=
not equal to
> ,<
less than or greater than
> =
greater than or equal to
<=
less than or equal to
Boolean operators
AND and OR lets us combine these relational operators to ask questions, such as did the employee work more than 40 AND less than 60 hours this week.
&&
AND
||
OR
What are API’s in Java?
API stands for Application Programming Interface. In Java-speak, the APIs are a collection of packages that programmers can import into their programs.
What is a package in Java?
Inside each package there are interfaces, classes, methods, fields, and constructors.
There is a lot of functionality in these packages.
How to input the Scanner class from the Java util package?
import java.util.Scanner;
What does pseudorandom mean?
that the actual bits underneath the hood are not moved around in an equal manner.
nextInt(int n)
a method that generates a pseudorandom number from the Random class
Java if statement:
if(Boolean_expression) {
// Anything here will be executed if the expression is true
}
What is the correct way to see if the String object s1 equals “green”
if(s1.equals(“green”)) { }
Recall that the Java String object has a method called equals(), and you place what you are comparing between the parentheses of that method.
What are the three things a Java program should always. have?
main method
class
some comments
conditional statement not necessary
What is a switch statement in Java?
In Java, the switch statement evaluates the expression, and then goes to a position within the switch statement based on that value.
Data types that cannot be evaluated: doubles or floats.
Specific case statements are processed based on that value. If none of the statements match, a default option is required for any fallout.
Describe a while loop:
A while statement performs an action until a certain criteria is false. This condition uses a boolean. The code will keep on processing as long as that value is true.
What is an indefinite loop?
A type of while loop where you don’t know when the condition will be true.
A while loop runs code as long as the conditions is/are?
The loop will continue as long as the value is true; once they are false the condition will stop.
Describe a for loop:
A for loop is a counting loop, because it repeats a set of instructions for a set number of times. For loops are useful when you know when the condition will be met.
for (initial start point; stop conditon; amount to increment) {
instructions/code here;
}
Nested for loops:
a for loop within a for loop
nested for loops are usually declared with the help of counter variables, conventionally declared as i, j, k, and so on, for each nested loop
For each loop:
used mainly for looping through collections
How many times will the nested loop run?
for (int i=1; i<=3; i++) {
for (int j=1; j<=i; j++) {
}
}
The nested loop will run at least once for each pass through the main loop. When i is 1, j will run once; When i is 2, j will run twice (since it is set to run as many times as the value of i); when i is 3, j will run three times; for a total of six.
What is the problem with this code?
for (int i=1; i<=3; i++) {
for (int j=1; i<=3; j++) {
}
}
It could run indefinitely
Nested while loop:
a while statement inside another while statement. In a nested while loop, one iteration of the outer loop is first executed, after which the inner loop is executed. The execution of the inner loop continues till the condition described in the inner loop is satisfied. Once the condition of the inner loop is satisfied, the program moves to th
In the code below what gets printed after 2 : 5?
class Main {
public static void main(String args[]) {
int outerLoop = 2;
while(outerLoop <5) {
int innerLoop = 4; while(innerLoop < 7) { System.out.println(outerLoop + ': ' + innerLoop); innerLoop++;
}
outerLoop++;
}
}
}
2:6
import java.util.*;
public class Main {
public static void main(String args[]) {
int outerLoop = 3;
while (outerLoop < 8) {
int innerLoop = 5;
while(innerLoop < 7) {
if(innerLoop == 6) { break; } else { System.out.println(outerLoop + ":" + innerLoop); innerLoop++; }
outerLoop++;
}
}
}
}
Here the inner loop breaks at 6. So it prints only till 5. The outer loop starts at 3 and prints 3 : 5. The next iteration of the inner loop is 6, but at this point the inner loop breaks. So the execution goes to the next iteration of the outer loop 4 and the inner loop 5, and prints 4 : 5.
In a nested while loop what happens if there is a break in the inner loop?
A break statement in an inner loop causes the inner loop to stop execution if the condition in the break statement is satisfied.