Getting Started With Java Programming Language

Creating Your First Java Program

Create a new file in your text editor or IDE named HelloWorld.java. Then paste this code block into the file and save:

public class HelloWorld {

   public static void main(String[] args) {

      System.out.println(“Hello, World!”);

   }

}

Run live on Ideone

Note: For Java to recognize this as a public class (and not throw a compile time error), the filename must be the same as the class name (HelloWorld in this example) with a .java extension. There should also be a public access modifier before it.

Naming conventions recommend that Java classes begin with an uppercase character, and be in camel case format (in which the first letter of each word is capitalized). The conventions recommend against underscores (_) and dollar signs ($).

To compile, open a terminal window and navigate to the directory of HelloWorld.java:

cd /path/to/containing/folder/

Note: cd is the terminal command to change directory.

Enter javac followed by the file name and extension as follows:

$ javac HelloWorld.java

It’s fairly common to get the error ‘javac’ is not recognized as an internal or external command, operable program or batch file. even when you have installed the JDK and are able to run the program from IDE eg. eclipse etc. Since the path is not added to the environment by default.

Strings

Strings (java.lang.String) are pieces of text stored in your program. Strings are not a primitive data type in Java, however, they are very common in Java programs.

In Java, Strings are immutable, meaning that they cannot be changed. (Click here for a more thorough explanation of immutability.)

Comparing Strings

In order to compare Strings for equality, you should use the String object’s equals or equalsIgnoreCase methods.

For example, the following snippet will determine if the two instances of String are equal on all characters:

String firstString = “Test123”;

String secondString = “Test” + 123;

if (firstString.equals(secondString)) {

   // Both Strings have the same content.

}

Live demo

This example will compare them, independent of their case:

String firstString = “Test123”;

String secondString = “TEST123”;

if (firstString.equalsIgnoreCase(secondString)) {

   // Both Strings are equal, ignoring the case of the individual characters.

}

Live demo

Note that equalsIgnoreCase does not let you specify a Locale. For instance, if you compare the two words “Taki” and “TAKI” in English they are equal; however, in Turkish they are different (in Turkish, the lowercase I is ı). For cases like this, converting both strings to lowercase (or uppercase) with Locale and then comparing with equals is the solution.

String firstString = “Taki”;

String secondString = “TAKI”;

System.out.println(firstString.equalsIgnoreCase(secondString)); //prints true

Locale locale = Locale.forLanguageTag(“tr-TR”);

System.out.println(firstString.toLowerCase(locale).equals(

secondString.toLowerCase(locale))); //prints false

Live demo

To be honest, that’s a lot that we’ve considered. More topics will be discussed in later articles to come. Stay tuned.

Note: compiling code like this is appropriate for small one-person projects, and for once-off programs. Beyond that, it is advisable to select and use a Java build tool. Alternatively, most programmers use a Java IDE (e.g. NetBeans,eclipse, IntelliJ IDEA) which offers an embedded compiler and incremental building of “projects”.

Leave a Reply

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

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

error

Enjoy this blog? Please spread the word :)