Java: Variables: String

In this tutorial I will explain the String. To do that I will use this file.


package net.tutorial.src;

public class Tutorial 
{
public static void main(String[] args)
{
System.out.println(“Hello World!”);
}
}


Like all other variables a String has to be created somewhere inside of a class.
The String is the only type of variable you can use that is able to save more than one character. It’s used pretty often and in many different ways. You can store any character in here. Letters and numbers, although the you can’t use the numbers to count things with.
The reason why people use the String is that it is the only way to save more than one character and it is also commenly used in a lot of the basic Java methods. It is also used to give the location of a file.
If you want to use a String in java you need to code it like this.


String hello = “Hello World!”;


The first part in this line is the type of variable. In this case a String.
The second part in this line of code is the name of the variable. In this case hello.
The last part of this line of code are the characters in the String. In this case Hello World!
If you want to print out a number in the console you have to use a line of code like this.


System.out.println(hello);


Because this bit of the code is in the main method and the main method is static you have to add the word static in front of String. Why that is and how you should use that is something for a later tutorial though.


package net.tutorial.src;

public class Tutorial 
{
static String hello = “Hello World!”;

public static void main(String[] args)
{
System.out.println(hello);
}
}


 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">