Any Java programmers here?

Discuss stuff not about Indigo.
User avatar
dougal2
Developer
Posts: 2532
Joined: Wed Nov 15, 2006 8:17 am
Location: South London

Any Java programmers here?

Post by dougal2 » Wed Jan 16, 2008 2:17 am

It's about time I learned a new language. Java seems about right for the things I want to do right now, so that's what I'm going to learn.

I'm already very proficient in PHP (and related to it, JavaScript), and can do C++ fairly well, but I don't fully understand some of it's more advanced features. (things like class templating and other abstract things).

So, where is a good place to start?

Obviously to start with I will no doubt be thinking about Java in terms of trying to translate solutions I already know for PHP and C++, and this may not be a good thing. Any advice on this front?

Cheers,
d.

User avatar
oodmb
Posts: 271
Joined: Thu Oct 26, 2006 5:39 am
Location: USA
Contact:

Post by oodmb » Wed Jan 16, 2008 2:37 am

i can't say how "here" i really am, but i am a Java programmer. i started learning Java in a class, and we really weren't learning Java, we were learning logic flow and ultra basics. assuming you know how memory works in c++, you might want to go ahead and learn the basics of formating in Java, then learn how Java handles references and objects. the major differences that I've noticed between Java and c++ that will be easier is not having to worry about headers, not worrying about garbage collecting, and not worrying about compiling as much. the differences that you should make yourself familiar with are code format, how references work, and how inheritance works.

The best way I've found to learn programming in general is to pick a largish project that you are really thrilled about completing (for me this was building a renderer), program it, learn new stuff while doing it, and then reprogram it (or do something else).
a shiny monkey is a happy monkey

User avatar
dougal2
Developer
Posts: 2532
Joined: Wed Nov 15, 2006 8:17 am
Location: South London

Post by dougal2 » Wed Jan 16, 2008 2:53 am

Actually, I have a project in mind, but I think I need to know a few basics before I can start.

(OK, I've already done a bit of reading and messing about...)
I've chosen NetBeans as an IDE. It seems to work very well.

Is there any standards for things like how to name Classes?
What's with the "Package" thing?
Are there standards regarding package names?

Until I can understand how a project is built, I can't really go about building a project :x

Say then I've created a Class. How do I instatiate the class? or make calls to it? or access it's properties?

Sorry for torrent of questions, don't feel you have to answer them - as I said, I'm after some guides/tutorials that will tell me this kid of thing, and I don't knwo where to look yet.

User avatar
psor
1st Place Winner
Posts: 1295
Joined: Sun Jun 25, 2006 1:25 am
Location: Berlin
Contact:

Post by psor » Wed Jan 16, 2008 3:20 am

I would say IanT (droid) the coder of Radium could answer some of your
questions very well. Unfortunately he is quite busy with RL right now. ;o))

You could kindly ask for the sourcecode of Radium, because he'll opensource
it anyway ... but maybe this would be overkill, dunno. It's up to you! 8) ;)



take care
psor
"The sleeper must awaken"

User avatar
oodmb
Posts: 271
Joined: Thu Oct 26, 2006 5:39 am
Location: USA
Contact:

Post by oodmb » Wed Jan 16, 2008 4:32 am

first of all, try using eclipse instead of netbeans, you get a lot more useful features, even if the interface is slightly more complicated for a beginner.
-easier compile
-easier package view
-easier to create new classes and such
-code auto complete
-realtime error checking
(i don't know if netbeans has any of these)

a package is one of what many people say is the greatest advantage of java.
of course, you don't actually have to know what it is to create programs in java, but it can be useful to know for large programs. basically, its like a file system in Java where fields (the variables in a class) can be made visible only to those classes within the same package. Methods can be created that are visible to all packages that access those fields. in this way, if you decide to remove that field in that class, you will only have to edit the method accessing that field and not every access of that field in every class. this makes changing and extending code much easier. you don't really need it though. its also a handy way of organizing code into files so you can find classes easier.
naming packages does not really have any specific guidelines, although there are some conventions-
conventions-
package names should not have any capital letters
packages intended to have sub-packages should have as short names as possible
guidlines-
subpackages are specified by the name of the parent +"."+name of the package and reside within the parent's directory. (just like a file system except rather than backslashes you use periods)
package names can not have spaces or special characters

classes-
the most basic class in java is an extension of java's Object class. (all classes inherit Object at there highest root point)
classes act like variables

using them-
a class can be referenced like this:
"class name" "name of reference";
for a class Vector3:
Vector3 vec;
when you declare a class like this it is a reference to an object. however, in this case because the class has not been instantiated it is a null reference.

to instantiate it you do it like this:
"class name" "name of reference"=new "class name"(*arguments*)

the "new "class name"(*arguments*)" is a call to that class's constructor method that contains the arguments that you input.
a constructor is a method with no return type that has the same name as the class. you can have more than one constructor but they all must have different arguments (method IDs in java include that method's argument types and order so you can have more than one method with the same name as long as they have different arguments)

when you call new you create a new object. in java, rather than creating a new object and passing it along to a reference, you can also set one field equal to the reference of an already existing object along to another reference for example:

Vector3 vec=new Vector3(3, 3, 3);
Vector3 vec2=vec;

in this case, a call to a void method like
vec2.add(vec2);
would edit both vec and vec2 because both are references to the same object.

other ways to instantiate an object would be:
Vector3 vec;
vec=new Vector3(3, 3, 3);

Vector3 vec2;
vec2=vec;

in a method, method fields can not be edited or used until they have been instantiated. however, class fields can be used by methods if they have not been instantiated in the constructor. this returns a NullReferenceException. just make sure you insantiate the feild before you use it.
a shiny monkey is a happy monkey

User avatar
dougal2
Developer
Posts: 2532
Joined: Wed Nov 15, 2006 8:17 am
Location: South London

Post by dougal2 » Wed Jan 16, 2008 6:16 am

oodmb:
That all pretty much makes sense. I shall try creating a few classes later and see if I can build a basic app that calls them.

Regarding IDE, well I've been using Eclipse at work for HTML/PHP development for a while now and I have begun to find it bit clunky/quirky, and not too good at moving files about ("Resource is out of sync with the filesystem" !!???! grr I hate that message, I have to manually refersh the file list in the source and destination folders before I can move files about.).

Netbeans so far seems altogether more integrated and has all of the same functions (+more, in fact having SVN integrated is a big plus). In fact I'm starting to use it for PHP coding - it works very well.

User avatar
eman7613
Posts: 597
Joined: Sat Sep 16, 2006 2:52 pm

Post by eman7613 » Thu Jan 17, 2008 5:43 am

i do a lot of stuff in java, so i figure ill drop my advice for strange gotchas.

1.) There are tons of pre created classes for you, from zip and tar balls to cryptography. You dont HAVE to use them, but they are nice to have for working on other stuff.

2.) Java wants you to handle most every posible exception. if you dont feel like using try catch, or are just trying to play & learn with a new API, you can add (my personal favorite) throws Throable to the method header and java will shut up about everything.

3.) Jars are basicaly zip files. You may notice (especialy with netbenas and importing a library) that you have multiple jars for a signle program. This is not nescicary, they can all be put in one jar, it changes nothing.

4.) String is a class, treat it like one (that means no ==).

5.) Integer != int, Double != double, excetera.

6.) becasue everything inherits from Object class, everything has a clone() method. Dont use it unless YOU wrote it yourself. clone() is usualy a shallow copy, and the same thing can be done with just the = operator.

7.) not only are there documents for just about anything anyone creates in java, but they are general update and worth reading.

8.) since your coming from c, there is no multiple inheritance, there are no multple return values, you do not have all the sysem memory, you run in your own sandbox with your own preset amount of memory.
Yes i know, my spelling sucks

User avatar
oodmb
Posts: 271
Joined: Thu Oct 26, 2006 5:39 am
Location: USA
Contact:

Post by oodmb » Thu Jan 17, 2008 10:10 am

the only ones of those i disagree with is 4 and 5

String, Double, Integer, Character, Short, Long, and Float are "special" java classes that do not behave like other java classes. a primitive type and its Object type are almost synonymous. it is true that there is no primitive type for String, but the operators still work on string as if it was a primitive type (==, +, !=). on these special classes that go along with there primitive types, the operators that work on there primitive types work on them the same way. in fact, if an operator can be done on that class and its primitive type, they can be done on each other producing either response.

operators and classes:

Integer (*, /, +, -, ++, --. ==, <, >, <=, >=, !=, <<, >>, <<=, >>=, >>>, >>>=)

Short(*, /, +, -, ++, --. ==,<, >, <=, >=, !=, <<, >>, <<=, >>=, >>>, >>>=)

Char(*, /, +, -, ++, --. ==, <, >, <=, >=, !=, <<, >>, <<=, >>=, >>>, >>>=)

Long (*, /, +, -, ++, --. ==, <, >, <=, >=, !=, <<, >>, <<=, >>=)

Float (*, /, +, -, ++, --. ==,<, >, <=, >=, !=)

Double (*, /, +, -, ++, --. ==,<, >, <=, >=, !=)

in equations you can treat the above types as primitives
EX:

Float f=5.0f;
Double d=5.0d;
Integer i=5;

double idf=f*d*i;
Double idfd=f*d*i;
(Note: when you operate on two primitive objects of different type, typecasting does not work, so the field has to be of the highest order used)


other operator pairs:

String (+, ==, <=, >=, !=)



play around with this, it will make sense eventually
a shiny monkey is a happy monkey

User avatar
eman7613
Posts: 597
Joined: Sat Sep 16, 2006 2:52 pm

Post by eman7613 » Fri Jan 18, 2008 7:01 am

@String (+, ==, <=, >=, !=)

no, the only thing that works properly for string is "+". using things like == with string will give you the wrong result.
Yes i know, my spelling sucks

User avatar
oodmb
Posts: 271
Joined: Thu Oct 26, 2006 5:39 am
Location: USA
Contact:

Post by oodmb » Fri Jan 18, 2008 8:16 am

try plugging this in, it gives you the correct result every time (== is case and space sensitive)

String str="flap";
String stra="flip";
String flo="flap";
System.out.println(str==stra);
System.out.println(str==flo);
System.out.println(str=="flip");
System.out.println(str=="flap");
System.out.println(str=="Flap");
a shiny monkey is a happy monkey

User avatar
ViennaLinux
Posts: 191
Joined: Thu Jul 26, 2007 9:26 am
Location: Vienna/Austria
Contact:

Post by ViennaLinux » Fri Jan 18, 2008 9:53 am

Code: Select all

String bla = "blub";
boolean wtf = (bla.equals("blub"));
Core2duo e6600 @ 3.1GHz watercooled at default VCore ^^

User avatar
oodmb
Posts: 271
Joined: Thu Oct 26, 2006 5:39 am
Location: USA
Contact:

Post by oodmb » Fri Jan 18, 2008 10:18 am

that should also work.
a shiny monkey is a happy monkey

User avatar
dougal2
Developer
Posts: 2532
Joined: Wed Nov 15, 2006 8:17 am
Location: South London

Post by dougal2 » Fri Jan 18, 2008 10:26 am

small problem, if I may...

Code: Select all

public class Image {
 int width, height;
 public void Image (int width, int height) {
  this.width = width;
  this.height = height;
 }

 public void someMethod()
 {
   /* .. some code .. */

   Image out = new Image(5, 5); //problem line

   /* .. some more code .. */
 }
}
what's wrong with my problem line? I get the error

Code: Select all

/path/to/Image.java:145: cannot find symbol
symbol  : constructor Image(int,int)
location: class org.graphics.Image

User avatar
dougal2
Developer
Posts: 2532
Joined: Wed Nov 15, 2006 8:17 am
Location: South London

Post by dougal2 » Fri Jan 18, 2008 10:29 am

oh, nevermind - my constructor was declared void. :oops:

User avatar
eman7613
Posts: 597
Joined: Sat Sep 16, 2006 2:52 pm

Post by eman7613 » Sat Jan 19, 2008 10:31 am

oodmb wrote:try plugging this in, it gives you the correct result every time (== is case and space sensitive)

String str="flap";
String stra="flip";
String flo="flap";
System.out.println(str==stra);
System.out.println(str==flo);
System.out.println(str=="flip");
System.out.println(str=="flap");
System.out.println(str=="Flap");
no, this is the wrong way to do it. While it works in THIS case, it will give you wrong results. To make my point add this

Code: Select all

flo = flo+"";
System.out.println(str == flo);
and now it is false instead of true, despite each being "flap". str.equals(flo) will properly return true.
Yes i know, my spelling sucks

Post Reply
28 posts

Who is online

Users browsing this forum: No registered users and 40 guests