Char Data Type
The char data type represents a single character.
char ch = 'y';
Instead of double quotes, it only uses single quotes in Java language. Double quotes are for strings while single quotes are for characters.
Only one character can go inside a single-quote structure.
char ch = 'a';
is okay, but
char ch = 'ab';
is not okay.
However, even though escape sequences have two characters for execution, it is still considered one character.
Characters and integers are closely related, so each character has a corresponding numeric value, vice versa. It is displayed on a ASCII table.
To switch characters into integers, type-casting should occur.
If the code was
int x = 76;
System.out.println((char)x);
The output would be the character corresponding to the intger 76, which on the ASCII table considers L.
An integer variable could be also created and initialized with a char value. However, in order for the output to be the corresponding integer for the letter, type-casting int is necessary.
int x = (int)'A';
System.out.println(x);
Therefore, the output is the integer corresponding to the character A, which is the integer 65.
Note that uppercase letters and lowercase letters have a different ASCII value. In fact, if uppercase A was subtracted from lowercase a, the difference would be 32. The same difference for uppercase B and lowercase b, and so on.
int difference = (int)'a' - (int)'A';
System.out.println(difference);
Output would be 32.
Methods
An important part of computer programming is code re-usability. It would be useful to only have to write some code once, then use it multiple times in multiple places in the program.
To do this very thing, Java language provides methods.
A method is a piece of code which, after written, can be used any number of times. They can take any number of parameters, and also return a value depending on the method type.
Examples of some methods that have been used so far is println, math class methods, etc.
So far, the only method that has been written in DrJava is the main method. That is because Java runs this method automatically without having to call the method.
Method Structure
An example of a created method is the following:
For now, always place "public static" before the method type and method name.
With the method, printMessage(), its return type is "void", because we don't want to return a value. This means there is no parameters inside the parentheses of this method. If it were int, float, double, etc., there would be a return statement and parameter values.
The method name is printMessage, which will be used to identify this method when "calling" it in the main method, as seen above. When calling the method in the main method, Java will run whatever code is inside the created method. In this case, Java ran the println message, because that statement was in our printMessage method.
In good programming, a method should try to do only one function rather than multiply functions at one run. And its length should be approximately one screen-length.
Using One Parameter
Instead of having the message inside the method, we can make it where the message is place inside the call of the method. To do this, you set the printMessage's parameters by adding one String parameter with its parameter name.
public static void printMessage(String msg)
{
System.out.println(msg);
}
This means that, whatever is placed inside the parentheses of the call of the method in the main method, that information is converted into a String variable called msg. And just like using variables to simplify the other statements, println will print out whatever "msg" equals.
So if (inside main method)
printMessage();
is
printMessage(This message was created outside of printMessage's method!);
Then the String variable, msg, is assigned these words and printed out in the output.
Using Two Parameters
If we wanted to have a message print out a name and a number, we would set two parameters in the printMessage method, one that is a String and one that is an int, double, etc.
public static void printMessage(String msg, int num)
{
System.out.println(msg + num);
}
Now an integer that is placed after a string message inside the parentheses of the called method printMessage (shown below) will be assigned the integer variable num.
The printMessage method is then run and prints out a string of the two variables concatenated.
So if (inside main method)
printMessage();
is
printMessage("hello ", 123);
Then the String variable, msg, is assigned the words 'hello', and the integer variable, num, is assigned the numbers '123'. They both will be printed out in the output. To designate which input goes into which parameter, a comma is used to separate them.
Make sure to always pass the correct parameter values in the parameter.
Methods and Return Statements
In methods, depending on the return type, it can return that said type after the code inside is executed.
If a method were an integer return type, it would return an integer value, etc.
public static int Multiply(int x)
{
int m = x*3;
return m;
}
This method will take the integer value, x, that was inserted in the main method, use that value to create an integer variable, m, into a value, and return this value to the main method.
In order for the programming to display this result, the main method would generally have a println method to show this.
This returning a value inside a method is called a return statement. It basically tells DrJava to exit from the method and return whatever value is assigned to be returned.
If the main method just had
Multiply(3);
nothing would happen, because no variable was assigned to it, making the returned value lost.
Instead
int result = Multiply(3);
would be suitable, because the returned value from Multiply(3), which would give back 9, is then saved in the integer variable result.
However, when using the return type, void, it will not return any value, and therefore does not need a return statement or any variable assigned to it in the main method. Although you can use a simple return statement (return;) in a void method, it is generally useless to do.
With return statements, you can only return one value, and it doesn't matter which type of value. (boolean, int, double, etc.)
Scope
Variables inside one method are completely different, and inaccessible, in other methods, including the main method. These variables have separate scope from each other.
Note that val1 in the main method is completely different than val1 in the Value1 method. If you truly want a variable or value to be accessed in more than one method, the variable or value must pass through a parameter.
It is also good practice to never create a variable the same name as a method name, no matter where that variable is stored.
Scanners and Getting User Input
Instead of just giving output to the user in the console with the println method, it can also be possible to get input from the user in the console. This is called standard input, or console input, stdin. We do this using the scanner object.
However, scanner commands are not built-in like the println method, so it must be imported into DrJava first.
Above the program, it should have the import statement
import java.util.Scanner;
Now DrJava knows we want more commands in our program.
The java.util.Scanner library is just a library for scanner commands. There are many other libraries for many other functions.
Now you can create a scanner object in any of the methods in the program.
To create a scanner object with a variable called keyboard, its statement is
Scanner keyboard = new Scanner(System.in);
or
Scanner keyboard;
keyboard = new Scanner(System.in);
Next, we can create a variable that allows the scanner to be shown in the console.
String input = keyboard.nextLine();
or (using a declaring statement before assigning the variable)
String input;
input = keyboard.nextLine();
For this one, it is stored in a String variable, where nextLine() causes whatever is typed into the scanner to become a string.
For numeric data, it's
int input = keyboard.nextInt(); for integer data
double input = keyboard.nextDouble(); for decimal data
For conditional data, it's
boolean input = keyboard.nextBoolean(); for true/false data
For scanner input, it can be used many times after one call of the scanner object.
Scanner keyboard = new Scanner(System.in);
int i = keyboard.nextInt();
double d = keyboard.nextDouble();
This will cause two scanners to appear, one for integer input and the other for double input. Numeric data that is typed into the input is stripped out of the scanner by whitespace, while non-numeric data is stripped out of the scanner by line breaks.
Parse Method
It is very useful to convert string data, such as numbers stored in a string variable, to integer/double/long data by using the parse method. This is because often data files in a program will contain numeric data that must be converted into integers first.
String data = "1234";
int num = Integer.parseInt(data);
double num1 = Double.parseDouble(data);
long num2 = Long.parseLong(data);
However, the string data must be numbers, otherwise the parse method will crash.