Let's Learn Java II
h
AdCharity (1244)

OKAY! After a somewhat long break, the next learning java lesson is here. Obviously, I'm not expecting many people to come (idk why but tutorials over time... don't get followed.)

Intro to Java

More on Storing Numbers

In the last part, we talked about mainly storing integers. But what about other types, like floating-point or decimal numbers? There are two built in types in Java that store "real" numbers (there are imaginary numbers, I'll refrain from digressing too much). These types are float, which uses 4 bytes, and double which uses 8 bytes. If you had to classify them more deeply, type float is a single-precision number, whereas double is a double-precision number (basically double is more accurate).

Final or Constant Variables

Final variables are declared using the keyword final and refer to values that will not change. For instance, we could declare a final variable like so:

final int GRADUATION_YEAR = 2023;
final int GPA = 4;

It's worth noting that final variables are typically written in ALL CAPS. What's really neat is that you can declare a final variable without actually assigning it a true value immediately. For instance, we can do:

final int GPA;
if(<condition, more on this later>)
    GPA = 4;
else
    GPA = 0;

After the "condition" or if/else statements, GPA's value will be set with its value final.

Arithmetic Operators

Arithmetic operators are somewhat obvious - it refers to things like +, -,* (multiplication), /, % (mod; returns a remainder). However, operators work a bit differently in Java because of the variable types, so it's good to know some situations.
When you use arithmetic operators on two different number types (like int + double), the int is automatically made into a double, and the result is a double.
How do you use the % though? many people don't even know what it does, but it actually is very useful (for say, logging whether or not 10 turns have passed). % basically means what is the remainder of a division. For instance 7 % 3 will return 1; as 3 goes into 7 twice but has a remainder of 1.
If you divide two integers, you will not get the actual fraction, but rather a truncated version (if you recall, integers don't store the decimal). As such, doing something like 1/2 (assuming they are declared as type int) will actually return 0 and not 0.5. You can control this property quite easily - if you wanted to keep the decimal, you could simply do (double) 1/2 and return 0.5.
WARNING: While it may make sense to do this: (double) (3/4) it will still return 0. Why? That's because the 3/4 is computed first (by order of operations). As such, the double applies to a 3/4 that has already been computed as 0. Confusing yes, but you should probably be aware of this situation.

Relational Operators

Relational operators refer to ==, !=, >, <, <=, and >=. You can think of these as comparison operators.
== - equal to
!= - like in most languages, the ! means not. Therefore, != means not equal to.
> - greater than
< - less than
>= - greater than or equal to
<= - less than or equal to
When comparing an int type and a double type, the int becomes a double when comparing (like the arithmetic operators).
Relational operators can be used in "boolean expressions" - boolean meaning true or false. For instance, we could do something like this:

int gradYear = 2023;
int age = 9;
boolean ageLessThanYear = (age < gradYear);
//ageLessThanYear will return true.

You should be very wary when comparing float type values. Since float is pretty much the most accurate one can get in Java, comparing them directly to other numbers may not yield what you want. There's actually a lot more operators, so hang tight.

Logical Operators

! - means not
&& - and
|| - or
Logical operators are often used in conditional statements, such as

if (gradYear < 3000 && gradYear > 2000)

Assignment Operators

New programmers often use

if(gradYear = 2000)

Notice the "="? That's not good :( As we'll now learn, a single equal sign is used to ASSIGN values, not compare them (although most compilers will still run it withoiut a problem).
= - ASSIGN a value
+= - add a value; It can be used something like this y += 8 means y = y + 8
-= - subtract a value; a similar example to the one right above would be y -= 8 means y = y - 8.
*= - multiply a value (I hope you're getting the hang of this)
/= - divide a value
There's also %=, which is kind of confusing. It's essentially used in the same way, but a more clear example would be:

int n = 10;
n %= 10;
//Will return 1; it's the same as writing n = n % 10

You are allowed to CHAIN operators like these. Meaning you can write something like this:

int class, gradYear, grade;
//WATCH
class = gradYear = grade = 2000

The last operators are the increment/decrement operators.

Increment Decrement Operators

++ and -- are the only operators that can be used in this way. Essentially, ++n means n=n+1, whereas --n means n=n-1.

Output/Giving the Console LIFE

Outputs in Java (comparatively, I'd say its something like console.log or print) can be extremely simple. System.out.print is how you print something to the console. You are also allowed to use System.out.println, which simple prints an item and then makes a new line. You can print pretty much anything, from a string to a number.

System.out.print("lmao");
System.out.println("idk");
//prints lmaoidk

System.out.println("lmao");
System.out.print("This is more readable.");
/*
Prints:
lmao
This is more readable.
*/

System.out.println(5+2);
//prints 7.

//You can also do something like this:
int x = 12
System.out.print("5+7 is equal to: " + x)
//Prints out "5+7 is equal to 12"

3scape Characters

Escape characters are a backslash + character. It is used to print special characters. The main ones include \n, which is new line, \" double quote, and \\ backslash.

System.out.print("Lmao \n new line");
//prints 
/*
Lmao
 new line
*/

System.out.print("\"lmao\"");
//prints "lmao"

System.out.print("\\");
//prints a single \

Finally! THE LONG AWAITED... LOGIC

Logic

I promised it last time, so it's here.

Decisions

A program can make decisions as to what path to follow using if and if...else. There's also switch, but chances are you won't see those for... a while.

If and Else Statement

if(<condition>){
    <statements>
}
else{
    <statements>
}

If a certain condition is met, run this line of code. If those conditions aren't met, then run "else" instead.

Nesting

You can actually nest if statements. For instance, doing this:

if(<condition>){
    if(<condition>){
        //Woah now you need two conditions
    }
    //You can put mor code here
}

You may not actually need two if statements (although certain situations call for certain measures). You could also do this:

if(<condition> && <condition>){
    //Two conditions
}

, which is perfectly valid.
As a beginner, I highly suggest using curly braces { and } to organize your work. Without them, it may be confusing as to how code is organized.

int gradYear = 2001;
if (gradYear > 2000)
    if (gradYear < 3000)
        System.out.println("Your graduation is still lower than 3000 lmao");
else 
    System.out.println("Your grad year is greater than 3000!)

You'd expect this to print "Your grad year is greater than 3000!" if you follow the statements if your gradYear was say, in 5000. Following the path, we can say 5000 is greater than 2000, so go ot the next condition. 5000 isn't less than 3000, so we don't print out that our year is lower than 3000. On the other hand, where is our message saying that our grad year is over 3000? The answer lies in the fact that else statements without brackets are paired using the nearest unpaired if statement (not indents - indents are for python lmao). If that's not really clear, the else statement above is paired with the `if(gradYear > 2000) statement. For instance, if our gradYear was 1000, the program would print that our grad year is greater than 3000. We can easily fix this with brackets, such as:

int gradYear = 2001;
if (gradYear > 2000){
    System.out.println("your graduation year is greater than 2000!);
    if(gradYear < 3000){
        System.out.println("Your graduation is still lower than 3000 lmao");
    }
    else {
        System.out.println("Your graduation year is greater than 3000 !!!)
    }
}

Else if?

You can also use else if in javascript, meaning:

if (<condition>)
//can be followed by 
else if (<condition>)
//can be ended by
else{

}
//cannot be followed by more stuff cause else covers all other exceptions.

It is primarily used to separate a a bunch of if statements, and can be ended with a final else statement.

What's next - I'll be going over iteration/loops as well as making classes and objects (basically where the meat is). If you've actually read this, thanks :) and I hope you have fun programming whatever language you choose.

Experimenting with blockquotes...
Ooo this looks kind of cool :D

You are viewing a single comment. View All