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.
<
public class HelloWorld
{
Line 12:
}
}
</syntaxhighlight>
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 <
== Statements ==
Line 68:
Comments are written using two forward slashes
<
Comments can be written at the end of a line
<
Comments can also be written on multiple lines by starting with '''/*''' and ending with'''*/'''.
<
/*
Comment 1
Line 79:
Comment 3
*/
</syntaxhighlight>
A multi-line comment that opens with two asterisks
<
/**
* Like this
*/
</syntaxhighlight>
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
<
a = 5;
</syntaxhighlight>
Fairly simple, right? Here is where it can get a slightly more tricky.
<
int a;
int b;
b = 6;
a = b; //a equals 6
</syntaxhighlight>
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:
<
int a = 6;
</syntaxhighlight>
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:
<
int a = 9;
Line 208:
a*2; // evaluates to 18
a%4; // evaluates to 1
</syntaxhighlight>
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:
<
int a, b, c, d;
a = a-b/c*d;
</syntaxhighlight>
the Java language would use the order-of-operations in this case.
Line 224:
Example:
<
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>
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.
<
You can also print out multiple variables on the same line:
<
Note that this won't work for two literal numeric values:
<
The plus sign would be mistaken for the addition operator for adding numeric values. So to print out "5432", you would have to type:
<
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:
<
public class Multiplying
{
Line 350:
}
}
</syntaxhighlight>
== 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.
<
import java.util.Scanner;
public class GettingStringInput
Line 374:
}
}
</syntaxhighlight>
''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.''
<
Now, let's go on to asking the user to enter a number and doing calculations on it. Basically: <
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: <
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 <
<
int mine = Integer.parseInt( JOptionPane.showInputDialog( null, "Now I can set an int!" ) );
System.out.println( mine );
</syntaxhighlight>
And now, of course, you can do whatever you want with that...
|