Vous êtes sur la page 1sur 6

AP Computer Science Study Sheet

Terminal & Java Basics


Terminal Basics
username @ computer : ~ $
~ slot for directory, stands for home directory if left as is.
Commands:
ls lists files.
ls -l files in long form.
ls -a all files displayed, including hidden files, which are usually files the user does
not modify. (Any file that begins with a . is hidden.)
cd <directory> change directory.
cat <file name> displays a file in the terminal.
It is a text-only session, and the user cannot scroll.
Doing this to a non-text file will show numerical representations. ASCII code, for
example, uses numbers to replace characters.
less <file name> displays a file with scrolling enabled.
The last line on the screen is highlighted.
It starts at the top of the file.
No command prompt is available, so enter must be pressed to proceed to the next page.
Q quits the display.
It warns you if a file is binary.
grep <word> <file name> searches for a word within a particular file.
grep <word> * searches for lambda in the entire directory, excluding hidden files.
man <command name> displays the manual for a command.
cp <source of file> <destination of file> copies a file into a new file.
If you want the directory to be changed, you must specify.
If you just type a file name for the destination, the file will be copied under a different
name in the same directory.
If you are in a certain directory, and want to copy a file from a different one, you will
have to specify directories.
If the destination has the same name as a different file, it will overwrite that file.
pwd prints directory name.
mv <source of file> <destination of file> moves a file.
The source does not retain the file.
It can be used to rename a file, if you move it within the same directory.
Again, it overwrites if the destination has the same name as another file.
rm <file name> removes a file.
It drags it to the recycling bin and then empties.
mkdir <directory name> makes a directory.
Symbols:
~ home.
/ root.
.. up one directory level.
. current directory.
File extensions don't actually matter, as they are just an extension of the name of the file.
The actual file is stored within the meta data.
Java Basics
Java is...

An object-oriented language.
A compiled language.
It translates (compiles) human-readable source code to machine or binary code.
Using javac, source code (.java) is compiled into binary (.class).
All .class files are cross-platform.
Different hardware and operating systems can use Java.
Thus, different versions of Java are not necessary.
(Normally, an application rests above its operating system, which is above its hardware.)
Java, however, rests upon a jvm (java virtual machine) which is unique for the operating
system being used.]
javac compiles, and java runs.
Miscellanea:
Java can be memory intensive, because of its nature.
Some functions are imported (such as java.io.* and java.util.*), using the import command.
Every statement ends with ;.
Every statement not having a { or } ends in ;.
Commenting out text:
/* Many
words. */
// single line.
Javac ignores comments.
As long as .class files are in the same directory, they can reference one another's classes and
methods.
Objects
Java is object oriented, in that it is a collection of objects.
Objects have 2 parts:
Instance variables are what objects have and know.
Methods are what objects can do.
Methods are followed by parentheses (method( )).
A class is a blueprint for all objects of the same type.
An instance is a specific individual of a given class.
If objects are in the same class, methods will be the same, but instance variables will not be.
A filename always has to be the same as the class it describes (always capital).
Classes list all the instance variables, and then all the methods.
Primitive Data Types
Every java variable must have a specific type.
Data types are either classes or primitives.
Primitive data types are not objects.
They are used to represent basic value types, like letters and numbers.
Mathematical operators are not methods.
Type
Name
Size (bits)
Range

integers

byte

-128 127

short

16

-215 215-1

int (what we use)

32

-231 -231-1
( 2 billion)

floating point numbers


booleans

long

64

-263 263-1
( 1018)

float

32

7 digits

double (what we use)

64

14 digits

boolean

true, false

characters
char
16
(letters, digits, symbols)
Calculating range:
Minimum is (2(number of bits 1)).
Maximum is 2(number of bits 1) 1.
A bit is a single 1 or 0.
A byte is composed of 8 bits.
Java Programs
Java programs have reserved, key words.
These are terms that are part of the Java language.
They are not external.
For example: public, import, variable types.
Variables are used to refer to a piece of memory with a value.
Every variable must have a type.
When you type <type> <variable>, this is known as declaring the variable.
A variable always refers to the same location in memory, and thus has the same value.
Names:
No spaces.
Reserved words cannot be used as variables.
Letters and numbers can be used.
Underscores and caps can be used.
Camel case is used in names new words have capitals but no spaces, likeThis.
Class names also follow conventions.
They are usually in upper case.
They can be used as a types.
Method names, however, are lower-cased.
Literals are denoted by ' on both sides.
They are obvious constants.
They include numbers.
A variable, like pi, can be fixed.
Single characters (when in single quotes).
For example: 'a', 'R', 't', '3'.
If you add literals, you will get their corresponding values.
'a' + 3 adds 3 to the numerical equivalent of a.
a + 3 simply a variable.
'3' + 1 adds the values, BUT displays as a literal, thus you will get '4'.
Special literal characters:
These must be in quotes.
'\n' new line.
'\t' tab.
'\b' beep/

'\' escape character, tells program to look at secondary interpretation.


They are used in the program, and not in the code.
String literals have one or more characters inside .
These do not act as normal literals.
Symbols used:
{, }, (, ), [, ], ;
Mathematical operators include +, -, /, *, % (mod).
Methods
The first line of a method is always the header.
<protection> <return type> <name> ( <parameters> ).
Level of protection protects certain values of an object.
It cannot be accessed from objects of a different type.
But it can be accessed from objects of the same class.
So you can simple refer from one instance of an object to the variable of another
instance of the object within one class. For example, otherBankAccount.balance.
Instance variables are usually private, while methods are usually public.
Return type states which type, if any, of primitive type or class will be returned.
All methods must have a return type, but not necessarily a value.
Void can be a return type.
Usually, methods that change something, or arrange something, will have a void return
type.
In void methods, simply writing return signifies the end of the method.
Returned values are stored, but not displayed on screen unless specifically programmed.
Returned values are used for further computing.
Display must be mentioned in a different part of the code.
The word return is used to return a value.
Return method is done, and will not continue.
Name of the method:
No spaces.
No Java keywords.
No variable already used can be used for a different purpose.
If there are multiple parameters, they look as such: (parameter 1, parameter 2, ...).
Commas are used.
Types must be given.
( ) no parameters.
The header goes before these brackets: { }.
Multiple methods can have the same name as long as the parameters are different, which is
known as overloading.
If two methods take the same input, but different types, the more precise method will be
used (double over int).
Order of variable input matters in determining uniqueness of methods.
There are special methods.
Constructor creates a new instance of an object.
It allocates memory for the object.
It sets initial values for the object, whether default or specified.
If there is no constructor for a class, a default constructor is provided by Java.
There can be more than one constructor for one class, as long as they have different
parameter sets.
The name of a constructor is the class name.

There is no return type, as the return type is the class itself.


this. refers to current object it is a variable keyword.
A static method is one that cannot be used by an object.
Main method the only method that automatically runs.
Every class can only have one main method.
The header will always read public static void main(String[] args).

Variables
A variable must first be declared.
They can only be declared once, as variables cannot be reused as other data types.
Primitive variables must be given a value before being used, which is known as initialization.
For example, int x = 10 sets the value of x to 10.
Objects must be instantiated, or an instance of it must be created.
A constructor is used for this purpose.
new is the keyword used to construct.
A variable is used to identify memory.
It is initially null.
After instantiation, it refers to a particular piece of memory.
An object is necessary to call a method.
. is used, meaning member-of operation.
z.teach( ), for example. Teacher z uses the teach method here.
String is a preset Java class.
A string is a group of 1 or more characters.
Even though it is not a primitive, a string can be initialized.
Strings can be combined with +.
For example, a = b + hi. Or, a = my + pin will put the number into the string.
Methods to print strings:
System.out.print( ).
System.out.println( ).
When an object is treated as a string, Java will look for a toString( ) method.
By default, treating an object as a String results in <name of class> @ <memory
address>.
The scope for a parameter variable is in the method itself.
It will cease to exist outside that variable.
This is different from instance variables.
Dividing an int by an int, despite resulting in a double result, will be displayed as an int.
Java will go to a more precise type. (double + int = double).
Type casting tells Java to treat a value as different from its native type.
Though Java always upgrades to a higher precision type, and yields an error if one would
get a less precise type, a less precise type can be yielded.
Type casting works with primitive numeric types.
Objects primitives cannot be typecasted.
Failed example: double x; int y; y = x; (an int cannot be set to a double value because this
will result in a lowering of precision).
This will result in an error message upon compiling.
Correct example: y = (int)x;.
This views the double x as an int, and allows a lowering of precision.
Random Math.random( ) returns a double in the range [0, 1).
If double g = Math.random( ); int x = (int) (Math.random( ) * 4), this will randomly result in
the integers 0, 1, 2, or 3.

Default values for non-primitive values are assigned by the default constructor.
Numbers 0 or 0.0.
Characters null character (' '), which can also be represented as '\0', which has the value
of 0, and is not the digit, which actually has a higher value.
Booleans false (0 = false).
Objects null, will not be assigned memory until instantiated.
Conditional Statements, Booleans, & Comparison Operators
Conditional statements control structures:

if (test) { result }.
If the test is true, then the result is performed.
Otherwise, it is skipped.
If (test) { result 1}
else { result 2 }.
If test is true, result 1 is performed.
Otherwise, result 2 is performed. (Else must directly follow.)
if (test 1) { result 1}
else if (test 2) { result 2 }
else if (test 3) { result 3 }...
The first true test will have its results performed, and the rest will be ignored.
Adding an else at the very end is optional.
Boolean operators and/or/not.
|| or.
Binary, as it works on 2 statements.
&& and.
Binary.
! = not..
Unary, as it negates one thing.
Boolean operators short-circuit.
Or returns a true at the first true.
And returns a false at the first false.
Comparison operators > , < , >= , <= , == , !=
They are used for number primitives and characters.
They are not used for objects, as objects are more than single values.
2 Strings cannot be compared with ==.
This is because 2 strings may have the same value but they refer to different pieces of
memory, and thus would not be considered equal.
In order to check if s1 represents the same string as s2, one must use the equals method.
For example, s1.equals(s2).

Vous aimerez peut-être aussi