Java – From a PHP programmers perspective

Java has one heck of a complex “Hello World”, intimidating many away from it. However as I had no choice but to learn it as it was the language of 7/8 weeks of my summer programming class at K-State I had to start using it.

I do not see the “Hello World” that intimidating anymore, for the single reason is that I know what and why all those words are there at the beginning of the file, and the best way for you to get into it is to understand it.

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello World");

    }

}

The first line is public class HelloWorld. public simply means that from anywhere and by anybody this class can be called. The class simply means that it is a class, as all beginning files in Java will be. The HelloWorld I think should be pretty easy to understand. The second line, public static void main(String[] args)... is a bit more complicated. The public meaning that this method can be called from outside this class. static means that this method can be called without initializing the whole class as an object. void simply means that it returns no value, as methods in the future might, like int or boolean. The next part, main means that this method should be run whenever this program is run, for any Java program to run, there must be a main method. String[] args is probably the strangest part, String is a line of characters, like a word, sentence, anything really. In Java, this [] mean an array, so String[] args means that for it’s parameters it takes an array of Strings. However you won’t deal with this for a while in your Java career. System.out.println... breaks down to System, which is a package (a folder full of classes) and out is a class within the package that deals with output. Finally println is a method (function) which prints the line.

Understanding Strings

Ever wondered why in Java, you see things like int and double and they are lower-cased, but String is capitalized? All class names in Java are capitalized, and so must String be.

String myname = "Jordan";

You are creating a new object (unique version of a class) of the class String. With strings you can do so much more than a primitive type (int, boolean, short, etc.). As myname is an object, it has methods that uniquely work with it.

System.out.print(myname.length()); // prints number of chars in myname

System.out.print(myname.charAt(4)); // prints ¬Ďa' for the 5th char in it

There are many more options, but that will do for examples of how this is an amazing way to store Strings.

More on Classes and Objects

To help you differentiate between classes and objects, I offer the following example.

MathOps.factorial(5); // uses the class MathOps and static method factorial()

MathOps math = new MathOps();

math.factorial(5); // uses the unique object

// another example that shows benefits

ShoppingCart cart1 = new ShoppingCart();

ShoppingCart cart2 = new ShoppingCart();

cart1.add("blanket");

cart1.add("shampoo");

cart2.add("ice cream");

cart1.remove("shampoo");

System.out.println(cart1.list());

System.out.println(cart2.list());

Yes, I have not actually made any of those classes, but those were just for examples, see with the ShoppingCart example, you saw exactly the benefits of the different objects as opposed to storing them in one big class where it would get all mixed up together.

User Input & Reading Files

It is quite strange, but in Java these are EXTREMELY similar so they shall be grouped together. To read user input in Java, one must first look for it.

Scanner s = new Scanner(System.in);

Scanner is a great class. I used to oppose it, but now seeing it’s virsitility, it’s a great class. Anyways what this says is create a new object named “s” that reads System input. To get a line of input is as easy as s.nextLine();

System.out.print("Please enter your name: ");

String name = s.nextLine();

System.out.println("Hello " + name);

Now the reason it was grouped with reading files is because if you want to ready a file, just tell Scanner so.

Scanner s = new Scanner(new File("filename.txt"));

while(s.hasNext()) { // hasNext() returns true as long is there more lines

    System.out.println(s.nextLine());

}

That would print out every line of the file. It is just like reading input, just a different start.

Loops and If’s

If your familiar with PHP this is extremely easy and only requires a paragraph or two.

boolean a  = true;

if(a) {

    // then do this

} else if(!a) {

    // then do this, notice the space between else and if not elseif

} else {

    // else do this

}

if(a) {

    // one line only can use this format

} else {

    // same for loops too

}

for(int i = 0; i < 10; i++) // you can use {} if you want

    System.out.println(i);

while(true) {

    // infinite loop

}

Good Things to Know

Integer.parseInt("123"); // can take a string (like user input) and convert it

Double.parseDouble("2.3"); //ditto

String b = "hey";

b.charAt(0); // gets the a (parameter) character from a string

Conclusion

With just that basic two to three page intro, you should be pretty set. Using just the things desribed here, you can do just about anything text-based. If you want to start working with GUI’s you’ll have to go to google and find a nice tutorial on how to do that. It’s more work, but it is just an extension of this sort of thing.

Glossary

Method (Java) ~ Function (PHP)

Int ~ Medium ranged integers

Double ~ For numbers with a decimal

Boolean ~ True or false

  • Thanks for the nice article. I have a question. Is the main() method in a Java class like the __construct() method in a PHP class?

  • Guest

    I think, variable scope is completely boring. as default, variables live in the scope in which they are created.

  • Nice post. Now learn Python and write the article I've always wanted to write, but as "Python from a PHP Programmers Perspective." :)

blog comments powered by Disqus