Syntax/grammer Flashcards
The way byte code understands code (47 cards)
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.