Primitive data types
Primitive data types: A primitive data type specifies the size and type of variable values, and it has no additional methods. Comments
Comments: Comments are ignored by the computer, they are removed during compilation and exist simply to make the code easier for people to understand. General class definition and body
Classes: Classes describe all objects of a particular kind, and determine the fields, constructors, and methods these specific instances will all have. Class names: By convention, class names start with an uppercase letter (to distinguish from other names like variables and methods.** Classes and types: A class name can be used as the type for a variable. Variables that have a class as their type can store references to objects belonging to that class General constructor definition and body
Constructors: Constructors are responsible for ensuring that an object is set up properly when it is first created/that an object is ready to be used immediately following its creation. Initialisation: This construction process is also called initialisation. The constructor initialises the fields. Note: In Java, all fields are automatically initialised to a default value if they are not explicitly initialised (0 for integers etc.) General while loops
While loops: A form of indefinite iteration loop. Note: While the condition evaluates to true, then the body is executed; and once it evaluates to false, the iteration is finished Note: The condition could evaluate to false on the very first time it is tested. If that happens, the body won't be executed at all. Note: The while loop does not need to be related to a collection. Even if processing a collection, we do not need to process every element. General do-while loops
Do-while loops: A form of indefinite iteration loop, but loop is executed at least once. Note: While the condition evaluates to true, then the body is executed; and once it evaluates to false, the iteration is finished. Note: The while loop does not need to be related to a collection. Even if processing a collection, we do not need to process every element. Other keywords
[Note to self: these may be sorted later into other groups as I cover more similar material] Casting
Casting: Casting means to change a value from one type to a "corresponding" value in another type. Note: We can cast char values to their Unicode int values and vice versa. Math
Math: Math defines pretty much all mathematical functionality that you will ever need. Note: It has other methods that are not stated here. Note: All of the methods in Math are static methods. Changes to parameters, equality over objects
Final variables
Final variables: Final variables must be initialised immediately and can never be changed. Throwing unchecked exceptions
Unchecked exceptions: When an exception is thrown and it is an unchecked exception, the system halts with an error message. It is a standardized way to deal with errors to provide informative feedback. Error type: Used with client code is seriously wrong - attempts to use your methods incorrectly by passing incorrect parameter values. |
Object data types
Object data types: Data types that are actually objects, which contain methods that can be called to perform certain operations on them. Logical operators
Precedence: && has a higher precedence than ||. Arithmetic operators
Arithmetic operators: Used to perform common mathematical operations. Precedence: Precedence and associativity are as normal as in maths. General method definition and body
Methods: Objects have methods that we use to communicate with them. We can use a method to make a change or to get information from the object. Conditional statements/ if-else-statements
Note: It is possible to have only 1 if-statement and no else if or else statements. There can also be many if-statements and else if statements in the same block. Importing library classes
Note: Usually happens at the very top of the program. Note: Using * means that all classes in that package is imported. Note: Some library classes are imported automatically, including Math, String, Integer, Character, Boolean etc. Creating objects
Note: Done in the constructor. Can also create an object assignment to a field, making the field point to the object, but the field variable will have to be declared first Note: If you haven't called 'new', you haven't created an object. General for-each loops
For-each loops: A type of definite iteration. Note: For each element in collection, execute loop body. Note: The new local variable ('element') used to hold the list elements in order is called the 'loop variable' (any name possible). The type of the loop variable must be the same as the declared element type of the collection. Note: We cannot change what is stored in the collection while iterating, but can change the states of objects already within the collection. General for loops
For-each loops: A type of indefinite iteration. Note: For each element in collection, do the things in the loop body. Conditionals: Conditionals/if-statements can be used in loops. General JUnit test
JUnit tests: JUnit classes run your code and compare actual results with expected results. Note: Some limitations are that - printing can't be tested; can test only changes to an objects state/values returned by methods; can test only 'public' methods; can't see inside methods. Static methods
Static methods: In static methods, the values returned don't depend on the state of an object, only on the arguments provided i.e. you can call a static method without creating an object first. (In fact it is sometimes impossible to create an object) Note: They are sometimes called class methods. Note: Can be invoked with the class name, rather than an object name. Dealing with arrays
Arrays: Arrays are fixed-size collections that can store object references or primitive values. It is an indexed sequence of variables of the same type. Note: The variables do not have individual names. Referencing elements: Elements can be used in the same ways and in the same contexts as any other variable of that type. Note: Arrays can share memory - 'Aliasing'. Objects: When using an arrays with elements of object type, you also have to populate the array with a loop. Making assertions
Assertions: A debugging mechanism to use when you are developing complicated code. When the assertion is executed, the boolean condition is evaluated. If it is true, execution continues. If it is false, execution is halted with an (unchecked) AssertionError, and the message string is printed. Error types: Logic errors. Check what values a given variable has compared to what it should have. |
Relational/Comparison operators
Relational operators: Operators used to compare two values - usually numbers, but also sometimes other types. Precedence: All have lower precedence than all arithmetic operators, and higher than all logical operators. Augmented assignment/ Assignment operators
Augmented assignment: Java supports augmented assignment for common arithmetic and logical operators. General field definition and body
Fields: Fields store data persistently within an object, that have values that can vary over time. Also known as instance variables. Every object will have space for each field declared in its class. General variable declaration and assignment
Variables: The basic mechanism by which data is organised and stored (long-term, short-term, and communication etc.). Variables must be declared before it is used. Variable names: Variable names should always start with a lower-case letter. Local variables: A local variable is defined inside a method body, as opposed to a field variable that is defined outside the method and a parameter that is always defined in the method header. Dealing with Strings
Strings: Strings are used to represent a sequence of characters, including: names, addresses, general text etc. They are written in double quotes. Note: String is a class defined in the library Note: Strings in Java are immutable objects (they cannot be changed after they are created). Ordering: Ordering is by the first letter in which they differ, otherwise by their length. (Note that it is based on Unicode values - not save for case and punctuation etc.) Method calls
Internal method calls: When an object calls a method on itself. In this case the object name will not need to be specified. External method calls: When one of the methods of the object in turn calls a method of another object to do part of the task. In this case the object the method is called on needs to be specified. Dealing with ArrayLists
ArrayList: A Java class from the java.util package. Dynamically sized collection, can store both object references or primitive values. Operations: - add: Adds the written object to the end of the collection. - size: returns an int of the collection size - get: retrieve an item from a specified index - remove: removes an item from a specified index. Will move up the indices of items behind it. [- others: search online] Chaining method calls
Note: This looks as if methods are calling methods, but the chain of method calls must be read strictly from left to right. Access modifiers
Public methods/variables: Public fields, methods and constructors in a class can be accessed/invoked from any class in the program. Private methods/variables: Private fields, methods and constructors in a class can be accessed/invoked only from the class where it is defined. Enumerated types
Enum: Used to represent discrete data with only a small number of possible values. Throwing checked exceptions
Checked exceptions: When an exception is thrown and it is a checked exception, the system tries to find some object in the program able to deal with it without crashing Note: More complicated than unchecked exceptions - method is required to declare it might throw exceptions; and client code is required to provide code that will be run if so Error type: Situations that are not entirely unexpected and from which clients may be able to recover. |
Cheatography
https://cheatography.com
Java - Beginner Syntax Cheat Sheet (DRAFT) by soleille01
A summary of all the Java programming language syntax I have learned so far. Still a noob so will have to be updated now and then.
This is a draft cheat sheet. It is a work in progress and is not finished yet.