Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views

Java Notes

This document provides an overview of key concepts in Java programming including primitive data types, variables, strings, operators, conditionals, loops, arrays, classes, methods, and objects. It defines byte, int, double, boolean, and char as primitive types and explains how to declare and assign variables. Strings are described as objects rather than primitive types. Operators like ==, !=, %, +, and - are also outlined. The basics of if/else statements, for loops, while loops, and arrays are covered. Finally, it introduces object-oriented concepts like classes, methods, and instance variables/fields.

Uploaded by

getrekt
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Java Notes

This document provides an overview of key concepts in Java programming including primitive data types, variables, strings, operators, conditionals, loops, arrays, classes, methods, and objects. It defines byte, int, double, boolean, and char as primitive types and explains how to declare and assign variables. Strings are described as objects rather than primitive types. Operators like ==, !=, %, +, and - are also outlined. The basics of if/else statements, for loops, while loops, and arrays are covered. Finally, it introduces object-oriented concepts like classes, methods, and instance variables/fields.

Uploaded by

getrekt
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 16

MANIPULATIN

G VARIABLES
QUICK LIST OF PRIMITIVE TYPES
IN CASE YOU FORGET AGAIN:
- byte [integer but has like no range;
-128 to 127]
- Int [integer, stores values of up to 2
million]
- Double [allows decimals, stores very
very large values]
- Boolean [true or false]
- Char [one character. Pretty useless if
you ask me, but whatever]

There is also short, long, and float for


numbers, but realistically you only need
to remember int and double.
System.out.println("whatever you want
to print");
The S in System needs to be capitalized
because for whatever reason, you will
get an error if it's not.

Variables can be started with a letter,


dollar sign, and an underscore.
Anything else is invalid. A variable is
declared by first declaring the primitive
type, and then giving it a name, and
then assigning a value to it. And always
make sure to remember the semicolon!

double aNumber = 89;

To declare a variable that cannot change


its value, use the keyword "final" before
the primitive type that you use to
declare what type the variable is. If you
try to change the value, this happens:
Final.java:5: error: cannot assign a value to final variable

SIDETRACK: Some symbols known as escape


sequences have alt uses in java print statements. The
compiler interprets these differently than regular symbols.
These escape characters begin with the character \ . There
are 3 main ones:

\" : Allows you to add quotation marks to a string value

\\: Allows you to place backslashes in the string

\n: Starts a new line after this is placed in a string

STRINGS:
Strings are objects, not primitive types.
You can use them in two ways: using a
string literal (just a sequence of
characters in double quotes) or
summoning the string class to create a
new string object.
String literal way: String greeting =
"Hello World";

Summoning String class to create new


string object: String greetingTwo =
new String("Hello Universe");

PRIMITIVE TYPE OPERATORS

Modulo (%) gives remainder of a


division of two numbers
Order of operations [in Java]:
Parentheses ()
Multiplication * and Division /
Modulo %
Addition + and Subtraction -
== is used to check the equality of two
variables, since = is used to ASSIGN
variables
!= is used to check the inequality of two
variables
>= greater than or equal to
<= less than or equal to
not: !
or: | |
and: &&

You can't use primitive type equality


operators on objects. To test equality of
objects, use a built in method called
.equals()

The plus sign can be used to combine


two Strings. For reasons beyond my
understanding, this is called
Concatenation.

OBJECT
ORIENTED
PROGRAMMIN
G
An object oriented programming
language allows programmers to
create their own template for
storing data; their own data type.
In OOP, a class is a blueprint of the
new data type that tells the
language what the data type looks
like/what it's made up of. It is the
set of instructions that describes
how the instances in it can
behave/what info they have.

An object is an example, or
instance, of a class. The class is a
blueprint, while the object is the
actual example of a piece of data
that is part of the class.

Before we cover classes, we need


to cover methods, loops,
conditionals, and arrays
IF STATEMENTS

if (boolean expression) {

}
else
{

FOR LOOPS
-Used when you know how many times
the loop should run

for () {

}
WHILE LOOPS
Loop keeps running until the condition
is met.
int n = 0;
while(n<9) {
System.out.println(n);

n++;
}

ARRAYS
<Type>[]nameOfArray = new
<Type>[size]

Example:
String[] names = new String[5]

for indexes they always start at 0


When you actually want to put stuff in
the list:
<Type>[] nameOfArray = {arrayItems}

String [] names = {"Jill"…<5 names


here>}

CLASSES
public class YerMom {
//methods, loops, and statements
public static void
main(Strings[] args){
//objects and other code go here
}
}

That is what a class looks like. The


top line is declaring a new public
class. A public class allows other
classes to access it. The line below
the class line ensures that the code
will compile. Without the public
static void main line, the code will
not compile. Any instances will be
written inside the second pair of
curly braces, while additional
methods, loops, and statements can
be written in the first pair.

METHODS
A method is a collection of
statements that perform an
operation. Basically, it is a
function. Here is what a method
looks like:

public Chair(double x, double y)


{

Functions have parameters, a


name, return type (any primitive
type, and the keyword void returns
nothing), and a body (which has
loops, conditionals, statements,
basically everything except
objects)

Inside the parentheses, you can


have Parameters, which are used
for variables. This is important,
because multiple parameters can
reside in the parentheses. These
parameters are separated by
commas. When an object calls the
method, it has the parentheses.
This is where you can plug in the
values of the parameters, and it
will allow you to perform the
function without having to declare
several variables.

DECLARING A VARIABLE
public class Car {
//constructor method (built in)
public static void
main(Strings[] args){
Car hondaCrv = new Car();
}
}

Refer to the code above to see how


to declare an object. There is a
built in constructor method before
main(). The constructor method
has the same name as the class.

When we made the object, we


assigned the Car instance to the
hondaCrv variable. Notice that the
type is not a primitive type. This is
because we are telling the
computer that the object is a
reference data type. A reference
data type is a data type where the
value of the variable is a reference
to the object's memory address
(what place in the memory is
located to store the object.)

After the assignment operator, we


are creating a new object within
the constructor method.

INSTANCE
VARIABLES/INSTANCE
FIELDS
Instance variables are the same
thing as instance fields. They are
variables inside of the class that
come before the main method. The
objects created within the class
will have these attributes, along
with the memory address.

LIBRARIES
in java libraries, they have free
stuff, which you can import. they
have classes and methods.
everything is prewritten.

You might also like