Learning Java/Basic Java Language: Difference between revisions

Content deleted Content added
m HTML5 cleanup
m Replace deprecated <source> with <syntaxhighlight>
Line 4:
Everything in Java is written in classes. A Class in this context is a section of code that can contain data (variables), and instructions for processing that data (methods). Returning to the Hello World example from the [[Introduction to Java]], the class is defined on the first line, and everything within the curly brackets is within that class.
 
<sourcesyntaxhighlight lang="java">
public class HelloWorld
{
Line 12:
}
}
</syntaxhighlight>
</source>
 
The first line is always of the general form:
Line 59:
 
Extends/Implements not complete yet. Soon..
Every java class has a parent class, if it has not been specified then the default parent class is <sourcesyntaxhighlight lang="java">java.lang.Object</sourcesyntaxhighlight>.
 
== Statements ==
Line 68:
 
Comments are written using two forward slashes
<sourcesyntaxhighlight lang="java">// This is a comment</sourcesyntaxhighlight>
Comments can be written at the end of a line
<sourcesyntaxhighlight lang="java">System.out.println("I love comments"); // This is also a comment</sourcesyntaxhighlight>
Comments can also be written on multiple lines by starting with '''/*''' and ending with'''*/'''.
 
<sourcesyntaxhighlight lang="java">
/*
Comment 1
Line 79:
Comment 3
*/
</syntaxhighlight>
</source>
 
A multi-line comment that opens with two asterisks
 
<sourcesyntaxhighlight lang="java">
/**
* Like this
*/
</syntaxhighlight>
</source>
 
is called a '''[[w:Javadoc |Javadoc comment]]'''. These comments are used to generate documentation for your code, and they generally appear at the beginning of a class or a method definition. A special program (also named javadoc) can be used to automatically create documentation from the Javadoc comments, usually in the form of a set of HTML files. It is generally good practice to fully document your code by using Javadoc comments, but it is not required. We will use comments to document and explain our code from now on.
Line 160:
 
The assignment operator, "=", sets the variable on its left equal to the value on its right. This code creates the variable ''a'' and sets it equal to 5
<sourcesyntaxhighlight lang="java">int a;
a = 5;
</syntaxhighlight>
</source>
Fairly simple, right? Here is where it can get a slightly more tricky.
 
<sourcesyntaxhighlight lang="java">
int a;
int b;
b = 6;
a = b; //a equals 6
</syntaxhighlight>
</source>
 
At first glance, you might think that the last statement is setting ''a'' equal to the letter "b", but instead it is setting ''a'' equal to the ''value'' of ''b'', which is 6. Essentially, a holds the value of 6. You can also set a variable and declare it on the same line:
 
<sourcesyntaxhighlight lang="java">
int a = 6;
</syntaxhighlight>
</source>
 
However, you can't say <code>char myChar = y;</code> to set a character to 'y'. That would set the character variable <code>myChar</code> to the value stored in the character variable <code>y</code>. To set <code>myChar</code> equal to the character <i>y</i>, use a '''character literal''' — a technical term for "a character in single-quotes": <code>char myChar = 'y';</code>. Note that a <code>char</code> primitive can only hold a single character. <code>char myChar = 'Yo';</code> is illegal.
Line 201:
=== Subtraction '-', multiplication '*', division '/', and modulus '%' operators ===
Just like the addition operator, the subtraction, multiplication, division, and modulus operators are used as follows:
<sourcesyntaxhighlight lang="java">
int a = 9;
 
Line 208:
a*2; // evaluates to 18
a%4; // evaluates to 1
</syntaxhighlight>
</source>
 
Subtraction, multiplication, and division, you have seen before, but the modulus operator is much less commonly used. Actually, it is commonly used, but is known under a different name, the remainder. Nine divided by four gives a remainder of one, so therefore 9%4 (pronounced "9 mod 4") evaluates to 1.
 
When more than one of the operators are used in the same statement, for example:
<sourcesyntaxhighlight lang="java">
int a, b, c, d;
a = a-b/c*d;
</syntaxhighlight>
</source>
 
the Java language would use the order-of-operations in this case.
Line 224:
 
Example:
<sourcesyntaxhighlight lang="java">
int a = 6; // assigns the value 6 to variable a
a += 5; // adds 5 to a, and assigns that value back into a, now a is 11
</syntaxhighlight>
</source>
 
The following lines are equivalent:
Line 325:
Using System.out.println() is extremely simple, though C/C++ programmers may find it cumbersome to type up compared to simply typing printf(). The method println() takes one parameter, the variable you wish to output, and writes it to the console.
 
<sourcesyntaxhighlight lang="java">System.out.println( myInt );</sourcesyntaxhighlight>
 
You can also print out multiple variables on the same line:
 
<sourcesyntaxhighlight lang="java">System.out.println( false + " " + 54 );</sourcesyntaxhighlight>
 
Note that this won't work for two literal numeric values:
 
<sourcesyntaxhighlight lang="java">System.out.println( 54 + 32 );</sourcesyntaxhighlight>.
 
The plus sign would be mistaken for the addition operator for adding numeric values. So to print out "5432", you would have to type:
 
<sourcesyntaxhighlight lang="java">System.out.println( 54 + "" + 32 );</sourcesyntaxhighlight>
 
Now you can do calculations and print the result! For example, say you want to evaluate 6*6*6*6 and then print the result ("1296"). Here is the code:
<sourcesyntaxhighlight lang="java">
public class Multiplying
{
Line 350:
}
}
</syntaxhighlight>
</source>
 
== Compiling and running your program ==
Line 362:
 
The first way to get user input is from the keyboard without using the swing gui. You must first import the Scanner package first like so. <code>import java.util.Scanner;</code> Once you have done that you will have to create a Scanner object. <code>Scanner scan1 = new Scanner(System.in);</code> Finally after the user has entered the input asked for you retrieve using your Scanner object. For example if we wanted a string as input we could use the following code.
<sourcesyntaxhighlight lang="java">
import java.util.Scanner;
public class GettingStringInput
Line 374:
}
}
</syntaxhighlight>
</source>
 
''In order for the following to work properly, you must place the following code on the first line of your .java file. Importing packages will be explained later.''
 
<sourcesyntaxhighlight lang="java">import javax.swing.JOptionPane;</sourcesyntaxhighlight>
 
Now, let's go on to asking the user to enter a number and doing calculations on it. Basically: <sourcesyntaxhighlight lang="java">JOptionPane.showInputDialog( null, "Hello, enter something" );</sourcesyntaxhighlight>. Try putting that in a class (in the main method, of course) and run it. Currently, we can't do anything with it because the value is deleted.
 
Because what you enter can consist of letters OR numbers, and any length of them, what is "produced" by that is a String. The following will work: <sourcesyntaxhighlight lang="java">String mine = JOptionPane.showInputDialog( null, "Now I can set a String!" );</sourcesyntaxhighlight>. Now lets say that after you get the string you want to print it. Just like regularly, add the following line: <sourcesyntaxhighlight lang="java">System.out.println( mine );</sourcesyntaxhighlight>.
 
Now you probably want to be able to enter a number. Java provides a method of changing a '''String''' to an <code>int</code>. It is <sourcesyntaxhighlight lang="java">Integer.parseInt( String_Here );</sourcesyntaxhighlight>. So lets say you want the user to enter a number and you print it. Do:
<sourcesyntaxhighlight lang="java">
int mine = Integer.parseInt( JOptionPane.showInputDialog( null, "Now I can set an int!" ) );
System.out.println( mine );
</syntaxhighlight>
</source>
 
And now, of course, you can do whatever you want with that...