Java - where to start.

Monkey Archive Forums/Monkey Discussion/Java - where to start.

Paul - Taiphoz(Posted 2012) [#1]
Sup all.

This year I plan to learn java, but starting with a clean slate, and knowing very little about it, where do I start ?

I need it all, I need tutorials on a good ide, and introduction to programming with it, hello world style, also with the intent to write games, so a game tutorial would be really good.

hope some of you have some advice and links, I installed net beans but its got so many options that without a guide I just felt swamped. :(


muddy_shoes(Posted 2012) [#2]
http://www.mindview.net/Books/TIJ/


therevills(Posted 2012) [#3]
I learnt (and I am still learning Java after years of using it*) Java using a text editor, I still think this is the best way to "learn" Java.

Once you know how it works, then learn an IDE like Eclipse or Netbeans.

* Yesterday I produced this code at work:

Float mass = bean.getMass();
if (mass < 1 || mass > 99.999) {
  displayMessage("Mass must be between 1 and 99.999");
}


Pretty simple, eh? But when I entered in a mass of 99.999 it displayed the error message... wtf!!
Samah pointed out that the literal value of 99.999 is a double and I needed to put it like this:
if (mass < 1 || mass > 99.999f) { 

D'oh!!!


Goodlookinguy(Posted 2012) [#4]
I'm in a Java class at the moment. I can tell you from taking this class so far that Java is an awful, awful language. I can't believe how many people like it. It just boggles my mind. If you plan to learn it, I strongly recommend finding a class at a college somewhere near by. Java has so many stupid little things in it that if you don't take one, it'll likely take you forever to learn about these stupid things. Sorry, this isn't really help, I just want you to be aware of some little stupid things I've come across. Good luck in learning it.

Here's the example:

public class Test
{
	public static void main(String args[])
	{
		System.out.println("Hello World"); // Hello World
		
		int intVar = 5;
		boolean boolVar;
		float fltVar = 5.0; // <-- I'm pretty sure this causes an error because 5.0 is a double
		float fltVar2 = 5.0f; // <-- Fixes that
		
		boolVar = (boolean)intVar; // <-- Error, can't typecast int to bool
		
		String str = "Hello World";
		
		if ( str == "Hello World" ); // <-- Checks if "Hello World" is in the same memory location as str
		
		if ( str.equals("Hello World"); // <-- Checks if str's value is equivalent to "Hello World"
		
		if ( 1.0 > 0.5 ); // <-- Those are doubles
		if ( 1.0f > 0.5f ); // <-- Those are floats
		
		if ( 10000000000000000L > 0 ); // <-- 0 is int coerced into long. L has to be appended for long.
		
		if ( 1 ); // <-- Error, can't coerce int 1 to bool
		
		if ( (intVar % 5) != 0 ); // Therefore you have to check for 0, which is stupid
		if ( !(intVar % 5) ); // <-- Most languages can do this, but not Java
	}
}



Dima(Posted 2012) [#5]
Java can't be that awful - I believe Monkey is inspired by it. Java is what you would call a 'forced methodology' language - try coding in C++ and then give Java another try :) Java is one of the best ways to learn OOP imo.


Samah(Posted 2012) [#6]
I'll agree that it punishes you sometimes, but I've caught many a runtime bug that a less strict compiler would have ignored.

float fltVar = 5.0; // <-- I'm pretty sure this causes an error because 5.0 is a double
Loss of precision can be a dangerous thing, so it gives you a compile error to be on the safe side.

boolVar = (boolean)intVar; // <-- Error, can't typecast int to bool
Because boolean is not a numeric type, and zero doesn't mean false (good, imo).
Edit: you'd do something like this:
boolVar = intVar!=0;

if ( str == "Hello World" ); // <-- Checks if "Hello World" is in the same memory location as str
As expected, but I'll agree it's annoying.

if ( str.equals("Hello World"); // <-- Checks if str's value is equivalent to "Hello World"
equals() does more than just check individual characters (different encodings and charsets, etc.)

if ( 1.0 > 0.5 ); // <-- Those are doubles
[quote]if ( 1.0f > 0.5f ); // <-- Those are floats
As in C, C++, Objective-C, and many other languages.

if ( 10000000000000000L > 0 ); // <-- 0 is int coerced into long. L has to be appended for long.
Correct, because that's too large for an integer. The 0 is automatically cast to a long for you.

if ( 1 ); // <-- Error, can't coerce int 1 to bool
Because 1 is numeric, not a boolean.

if ( (intVar % 5) != 0 ); // Therefore you have to check for 0, which is stupid
Maybe, but it's due to static typing.

if ( !(intVar % 5) ); // <-- Most languages can do this, but not Java
Because (intVar % 5) is numeric, not boolean.


Paul - Taiphoz(Posted 2012) [#7]
jesus guys you really know how to put a guy off. lol.


Samah(Posted 2012) [#8]
Pretty much all those complaints are due to static typing. Personally I hate dynamic types. Java is great for learning OOP, though.
Edited to be less ragey, sorry. <3
I'm just a very overprotective fanboy. :)
<3 Goodlookingguy

You have listed some important gotchas though.


Paul - Taiphoz(Posted 2012) [#9]
I still wana learn it tho, just need a good starting point, I have now tried to compile something with eclipse and net beans neither worked, so I think I need to get a book that will really walk me through the process.


Samah(Posted 2012) [#10]
Eclipse is pretty easy to set up once you get over the fact it has no installer (good and bad, really).
NetBeans is good too, but I prefer the interface for Eclipse. The NetBeans GUI designer is really nice though.


TeaBoy(Posted 2012) [#11]
I understand some concerns people have about learning Java, as it forces you to think in OOP, but IMO that is a GOOD thing. I have found the book Java How to Program by Deitel and Deitel (Ninth Edition) to be a decent book.


Gerry Quinn(Posted 2012) [#12]
I don't love Java, but IMO knowing Monkey will be a help. I know some folks disagree with me, but I think Monkey is a lot like Java in structure and Basic in syntax.


muddy_shoes(Posted 2012) [#13]
Monkey certainly has many Java-like features. Enough that it suffers through the comparison when it fails to leverage the level of thinking that has gone into Java's design, such as the auto-boxing rules and the collections framework.


Goodlookinguy(Posted 2012) [#14]
Samah, some of the things I was just pointing out. I wasn't necessarily against them. Java is the 34th programming language that I'e been learning. I'm an experienced programmer and I'm quite aware of similarities to other languages. That's why I've come to hate Java for little certain things in it just bother me like crazy. As well as the fact that because it's not machine code; It'll need the Java VM and that will, obviously, perform slower and won't run natively on systems.

I'd like to bring up this. Bools are ints. It's crazy to think otherwise.
boolVar = (boolean)1;
mov eax, 1

I've also done electrical engineering as well, 1 == true / on / closed, 0 == false / off / open.

Oh, and you should read up on the guy who coined the term OOP. He wanted programming languages to be more dynamic than php. It's actually quite interesting.

------
Taiphoz, I'll assume the Java SDK is installed. These are some examples of how to manually compile that should be at least sort of similar to your system(s) I hope.

In Windows Command Prompt
@SET java="C:\Program Files\Java\<SDK Path>\bin"
%java%\javac myJavaFile.java
%java%\java myJavaFile

In Linux
JAVA=/usr/java/<SDK Path>/bin
$JAVA/javac myJavaFile.java
$JAVA/java myJavaFile


I should mention that the class name MUST be the same as the file name. That's how Java knows where the entry point is. (Look at the example I wrote at my first post above and just remove the inside code for a starter file)


therevills(Posted 2012) [#15]
^ and thats why I said learn Java in a text editor not an IDE ;)


TeaBoy(Posted 2012) [#16]
Notepad++ and the command line is all you need to create with Java :)