String Literals
- Declare a class: public class "name"
- Use curly brackets in order to define where a class begins and where it ends
- Put methods (at least one) inside of the curly brackets representing the class
- Main methods: public static void main(String[] args)
- Another curly bracket to define where the method begins and ends -- indented this time
- System.out.println (""); -- lets us output text to our console, must end with a semicolon
public class Program {
public static void main(String[] args)
{
System.out.println("My name is Brian");
}
}
Program.main(null)
public class Variables{
public static void main(String[] args)
{
int x;
x = 90;
x = 5;
x = x + 1;
int Score = 90;
System.out.println("The score is: "+ Score);
}
}
Variables.main(null)
8 Primitive Data Types
- Byte: minimum value of -128 and maximum value of 127 (inclusive). It is useful for saving memory in large arrays
- Short: Minimum value of -32,768 and maximum value of 32,767. Same purpose as Byte
- Int: Any integer or whole number
- Long: Greater range than int
- Float: floating point numbers that tend to have decimals
- Double: not good for precise data
- Boolean: logic that evaluates whether a condition is true or false
- Char
public class PrimitiveDataTypes
{
public static void main(String[] args)
{
int a = 5;
double b = 5.0;
b = 3; // only works for double
a = (int) 5.999; // casting cuts off everything after the decimal
System.out.println(a);
boolean c = true;
c = false;
System.out.println(c);
}
}
PrimitiveDataTypes.main(null)
public class Operators
{
public static void main(String[] args)
{
int x;
x = 5;
System.out.println(5+2);
System.out.println(5-2);
System.out.println(5*5);
System.out.println(5/5);
x = 3*3;
x = 3 * x;
System.out.println(5 % 3); // 5 mod 3 finds the remainder when 5 is divided by 3
}
}
Operators.main(null)
public DownloadInfo getDownloadInfo(String title) { // define the class
for (DownloadInfo info : downloadList){
if (info.getTitle().equals(title)){
return info;
}
}
return null; // method inside the class
}
public void updateDownloads(List<String> titles) {
for (String title : titles) {
DownloadInfo foundInfo = getDownloadInfo(title); // calls method
if (foundInfo == null){
downloadList.add(new DownloadInfo(title)); // makes new object
}
else {
foundInfo.incrementTimesDownloaded();
}
}
}