Let's Learn Java I
Intro to Java
As the first of the upcoming series, I might as well preface why I'm starting Java tutorials. Frankly, I'm getting rather concerned about my life and coding career (mind you I'm not going to college until another 4 years), but when I found out that some programming languages have certain benefits, I figured I might as well learn them. I was rather estatic when I discovered Java, is the Computer Science AP test language. Easy money - learn java now and ace the AP test. While I'm at it, I'm going to share what I learned with you. I'll be pretending like you know NO CODING AT ALL, so this is kind of boring. But if you're interested in learning along with me, whatever floats your boat.
Don't blame me if you still can't code java - this stuff is confusing
Java was developed by a team at Sun Microsystems CA, most noteably a guy named James Gosling. While java has basic if-else statements and loops, the power of Java is found in objects.
Classes - has objects that interact with other classes
Packages - a bunch of classes, most of which are provided by the compiler
java.lang - contains most used classes; usually added to all Java programs
(obviously, replace anything in brackets with the appropriate class or package)
To import a class in a package, you must use:
import [package name].*;
To import a single class from a package, you must use:
import [package name].[class name];
Java also has subpackages, which you can access in a similar way.
import [package name].[subpackage name].[class name]
import allows the programmer to use objects and methods (or for JS people, functions) from other files written by yourself or other people.
All Java programs must have at least one class, what is usually called
main method (very similar to Python).
All Java methods must be in a class.
All Java statements must be in a method.
The main method doesn't have more methods in it.
main, among others are reserved words (or key words) that cannot be renamed. If the compiler for some reason allows you to rename them, DO NOT - as reserved words, they have there own respective tasks.
static refers to methods/functions that will not access objects of a class - the main method is always static.
Applications vs Applet
Applications are basically java code, saved as program.java - applets are programs that run inside a web browser/applet viewers.
Java commenting is kind of confusing in a way (but sometimes not). Commenting usually looks something like this (note the / and /):
/** * Eat cheese method * @param eat defines the type of cheese to be eaten * @param per defines the person to eat the type of cheese * @return rate of cheese eating * @throws no more cheese (ok a real error will probably be here) if no person eats cheese. */ //Also a comment
But what about the whole @param whatever thing? Basically
@throws are commenting terminology. As suggested by the name, it tells the user what parameters (stuff a function will take), the expected output, and any errors if something goes amiss.
identifier refers to a variable, parameter, method, class, or constant. It's made of letters, digits, and underscore characters, but cannot start with a digit. They are also case sensitive, meaning
hello are NOT THE SAME. Usually, they are called what they are supposed to do - for instance,
volume is probably better than say,
Identifiers are usually written in Camel Case (or underscores), which basically means you upper case words to distinguish them. For instance,
checkCubeSides is readable because we have used Camel Case to essentially show solid words.
Identifiers cannot be called by any reserved words (see Conventions).
All identifiers have a type - which makes it somewhat more complicated than most languages. Built in types include
double, and if you're feeling smart,
int - if you know math, its literally a positive or negative number that is not a fraction/contains decimals. Ints are kind of special in that they have a fixed amount of memory. If a value is too large in an int, you will likely get the wrong result WITHOUT warning.
boolean - true or false
double - a floating point number, basically something with a decimal like 2.239479823748923
Usually variables are declared by a type immediately. For instance, we can have
int countChars = 1; double y,z; boolean whyyyyy;
var covers pretty much everything. Here's something to chew on:
int num = 5; double perciseNum = num; //perciseNum is 5, but is a DOUBLE!!! It's kind of useful in certain situations it seems, and you don't have to explicitly state when you assign a double to an int value. HOWEVER... double num = 6.8; int secondNum = num; //DOES NOT WORK! You have to explicitly say this yourself, such as: double num = 6.8; int secondNum = (int) num; //This will work now. Note that secondNum will return 6, as it just removes the decimal part.
Now why is all of the stuff above (regarding defining doubles and ints) actually useful? If you wanted to round to the nearest integer, you could do something like this:
double integer = 5.123123123; int roundInteger = (int) (integer + 1) //Should return 6, no decimal.
Integers in Java are stored as string of bits - binary digits. Bits also store the sign of the integer (0 is postive and 1 is negative).
int uses four bytes, or 32 bits. The largest possible inteer is (2^31) - 1.
Random stuff (if this gets confusing forget it) - there are built in Java types besides int. For instance,
byte holds one byte,
short holds 2 bytes,
int holds 4 bytes, and
long holds 8 bytes. Of course, no one really needs to know that right now.
Next up: https://repl.it/talk/learn/Lets-Learn-Java-II/25046 (thanks @Highwayman)