Syntax/grammer Flashcards
The way byte code understands code
Assignment Statement
::===
ex balance= nuBalance
Message Statement
.([actual parameter]);
ex. myaccount.setBalance(1234)
Return statement
::== return[];
ex. return balance;
What does () mean? ex. account();
“This”
ex This account.
What is a class?
Template of an object (cookie cutter, press)
contains methods and variables.
What is a variable?
Variable contains information
What is a Method?
Perform actions, Allows classes to do things.
primitives in Java:
byte (number, 1 byte) short (number, 2 bytes) int (number, 4 bytes) long (number, 8 bytes) float (float number, 4 bytes) double (float number, 8 bytes) char (a character, 2 bytes) boolean (true or false, 1 byte)
To declare and assign a number
int myNumber;
myNumber = 5;
or
int myNumber = 5;
To define a floating point number,
float f = (float) 4.5;
syntax for chars
char c = ‘g’;
ways to use a string
// Create a string with a constructor String s1 = new String("Who let the dogs out?"); // Just using "" creates a string, so no need to write it the previous way. String s2 = "Who who who who!"; // Java defined the operator + on strings String s3 = s1 + s2;
Boolean
boolean b = false;
b = true;
boolean toBe = false; b = toBe || !toBe; if (b) { System.out.println(toBe); }
int children = 0; b = children; // Will not work if (children) { // Will not work // Will not work }
The Arithmetic Operators (5)
\+ additive operator (also used for String concatenation) - subtraction operator * multiplication operator / division operator % remainder operator
The Unary Operators(5)
\+ Unary plus operator; indicates positive value (numbers are positive without this, however) - Unary minus operator; negates an expression \++ Increment operator; increments a value by 1 -- Decrement operator; decrements a value by 1 ! Logical complement operator; inverts the value of a boolean
The Equality and Relational Operators(6)
== equal to != not equal to > greater than >= greater than or equal to < less than <= less than or equal to
The Conditional Operators(2)
&& Conditional-AND
|| Conditional-OR
instanceof <—-what does it do?
The instanceof operator compares an object to a specified type
Field declarations are composed of three components, in order?
Zero or more modifiers, such as public or private.
The field’s type.
The field’s name.
Defining Methods
1 Modifiers—such as public, private, and others you will learn about later.
2 The return type—the data type of the value returned by the method, or void if the method does not return a value.
3 The method name—the rules for field names apply to method names as well, but the convention is a little different.
4 The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
5 An exception list—to be discussed later.
6 The method body, enclosed between braces—the method’s code, including the declaration of local variables, goes here.
method Explanation “Public”
Specifies that the method is accessible from outside a class
method Explanation “Static”
Specifies that the method is a class method that is to be executable.even though no class objects have been created.
method Explanation “Void”
Specifies that the method does not return a value
Create an object syntax
ex. Point originOne = new Point(23, 94);
Declaration: The code originOne are all variable declarations that associate a variable name with an object type. Instantiation: The new keyword is a Java operator that creates the object. Initialization: The new operator is followed by a call to a constructor, which initializes the new object.
Constructor syntax
A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that they use the name of the class and have no return type. For example, Bicycle has one constructor:
public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; }
WHat is an Accessor?
The methods that give other objects ac- cess to variables inside an object are called “accessors.” ex: 1 public int getBalance() { 2 3} 4 5 public String getName() { 6 7} return name; // return the name
Converting a String to an int
int guess= Integer.parseInt(stringGuess);
turns a number 2 into string “2”.
Syntax for a cast Operator(change it from long to int)
long y = 42
int x = (int) y
Name of pre build classes in Java
API or Java Library
Arraylist: Adds the object parameter to the list
add(Object elem)
Arraylist: removes the object at the index parameter
remove(int index)
Arraylist: removes this object(if its in the arraylist).
remove(Object elem)
Arraylist:returns “true” if theres a match for the object parameter
contains(Object elem)
Arraylist: returns true if the list has no Elements
isEmpty()
Arraylist:Returns either the index of the object parameter,or-1
indexOf(Object elem)
Arraylist: Returns the number of elements currently in the list
size()
Arraylist: Returns the object currently at the index parameter
get(in index)
Define Arguments
Arguments are the things pass into the methods.
ex
d.bark(3)
3=argument/settings
Define constructor
Constructor is an object with arguments in them. Must be the same as the class name it is in. ex: public class TUNA{ public TUNA(String Name){ girlname=name;}
main class tuna tunaobject=new tuna(NAME)
Rules for Getters and Setter
Mark instance variables private
Mark getters and setter public.
What is a Instance Variable?
Instance Variable gets declared inside a class but not within a method.
What is a local Variable?
Local variables are declared within a method.
LOCAL VARIABLES do Not get default values.
wrong = int b;
right= int b=12
How do you save a file(4 steps)
1. Make a FileOutputStream FileOutputStream fileStream = new FileOutputStream(“MyGame.ser”);
2.Make an ObjectOutputStream ObjectOutputStream os = new ObjectOutputStream(fileStream);
- Write the object
os. writeObject(characterOne); os.writeObject(characterTwo); os.writeObject(characterThree); - Close the ObjectOutputStream
os. close();
What is overloading Method
Several methods same name different arguments
Api for buttons
Import Javax.swing.*;
API for listeners
java.awt.event.*;
How do you read files.(5 steps)
1. Make a FileInputStream FileInputStream fileStream = new FileInputStream(“MyGame.ser”);
2.Make an ObjectInputStream ObjectInputStream os = new ObjectInputStream(fileStream);
3.read the objects
Object one = os.readObject(); Object two = os.readObject(); Object three = os.readObject();
4.Cast the objects
GameCharacter elf = (GameCharacter) one; GameCharacter troll = (GameCharacter) two; GameCharacter magician = (GameCharacter) three;
- Close the ObjectInputStream
os. close();