Jump to content

Starting to dip my toes into Java...


nrg_alpha

Recommended Posts

Oh my.. this is going to be an adjustment.. I can just tell  :tease-03:

You get so accustomed to one language's syntax, then when you start playing around in anothers, you're head is spinning around like in the Exorcist (minus the green spewing puke - maybe).

 

I'm immediately thrown off by strings being objects as opposed to primitive types like int for example (yeah, I know you can use objects in place of those primitives too). I'm not sure why float types have to have the 'f' at the end of the value, like say float number = 4.67f for instance (you would think that simply 4.67 alone would suffice - at least I did)... Yep, it's going to be a bizarre ride initially for sure (buckles up). Should prove interesting to say the least! :thumb-up:

Link to comment
Share on other sites

Oh my.. this is going to be an adjustment.. I can just tell  :tease-03:

You get so accustomed to one language's syntax, then when you start playing around in anothers, you're head is spinning around like in the Exorcist (minus the green spewing puke - maybe).

 

I'm immediately thrown off by strings being objects as opposed to primitive types like int for example (yeah, I know you can use objects in place of those primitives too). I'm not sure why float types have to have the 'f' at the end of the value, like say float number = 4.67f for instance (you would think that simply 4.67 alone would suffice - at least I did)... Yep, it's going to be a bizarre ride initially for sure (buckles up). Should prove interesting to say the least! :thumb-up:

 

It's because the standard data type for representing floating point numbers in Java is called double. If you want to use float, which has only got half the precision, you'll have to explicitly specify that.

 

However, if you do something like this:

float foo = 1.5;

it will work as well.

Link to comment
Share on other sites

Oh my.. this is going to be an adjustment.. I can just tell  :tease-03:

You get so accustomed to one language's syntax, then when you start playing around in anothers, you're head is spinning around like in the Exorcist (minus the green spewing puke - maybe).

 

I'm immediately thrown off by strings being objects as opposed to primitive types like int for example (yeah, I know you can use objects in place of those primitives too). I'm not sure why float types have to have the 'f' at the end of the value, like say float number = 4.67f for instance (you would think that simply 4.67 alone would suffice - at least I did)... Yep, it's going to be a bizarre ride initially for sure (buckles up). Should prove interesting to say the least! :thumb-up:

 

This has thrown me off, as well.  I might make a small suggestion for you: learn javascript first.  Or if you know javascript, think of javascript as the Rosetta stone between java and php.  For me, learning javascript was a lot less painful because it is more directly web-oriented, but also has the Object-based programming (for example, that string-as-object thing you mentioned).  IMO this makes javascript a great learning-bridge between someone going from php to something like java.  Maybe that's just me, but dunno, maybe that might help.

Link to comment
Share on other sites

It's because the standard data type for representing floating point numbers in Java is called double. If you want to use float, which has only got half the precision, you'll have to explicitly specify that.

 

However, if you do something like this:

float foo = 1.5;

it will work as well.

 

Yeah, the Sun Java tutorial mentions not using floats for precision values (turns out apparently even double data types should not be used for precision [in this case, they keep referring to currency as an example] - there is a java.math.BigDecimal class for that purpose) For your example, I think you mean

double foo = 1.5;

(when delcaring a float type, it requires 'f' postfixing to the value).

 

 

This has thrown me off, as well.  I might make a small suggestion for you: learn javascript first.  Or if you know javascript, think of javascript as the Rosetta stone between java and php.  For me, learning javascript was a lot less painful because it is more directly web-oriented, but also has the Object-based programming (for example, that string-as-object thing you mentioned).  IMO this makes javascript a great learning-bridge between someone going from php to something like java.  Maybe that's just me, but dunno, maybe that might help.

 

Yeah, I did dabble a bit in javascript a few years back, but I don't really know it enough to be able to say I'm comfortable / knowledgable with it. Perhaps as I learn java more, when I look at javascript, it will be more familiar? Speaking of familiarity, I am noticing that java isn't totally foreign.. for, do-while loops, ternary operators, switch statements and the like seem to be pretty much like PHP's. So at this stage in the game, it's more or less getting to know java's nuances.

 

Link to comment
Share on other sites

right. you can certainly use java as a bridge to javascript just the same.  Was just throwing that out there that since you know php you could go from php to javascript to java.  Dunno your situation but in general javascript may be more useful to you, since it's more directly involved in web development.  because it is more directly involved in web development, you should pick up on the underlying structure (like the DOM) and point faster.  As you learn the DOM, you learn the things like string-as-object and then those will carry over to learning java, which is a much broader "stage" than just web development.  My theory is that you'd learn javascript faster than java because you already know php, so use that as a stepping stone to java. 

Link to comment
Share on other sites

It's because the standard data type for representing floating point numbers in Java is called double. If you want to use float, which has only got half the precision, you'll have to explicitly specify that.

 

However, if you do something like this:

float foo = 1.5;

it will work as well.

 

Yeah, the Sun Java tutorial mentions not using floats for precision values (turns out apparently even double data types should not be used for precision [in this case, they keep referring to currency as an example] - there is a java.math.BigDecimal class for that purpose) For your example, I think you mean

double foo = 1.5;

(when delcaring a float type, it requires 'f' postfixing to the value).

 

Hmm... yeah, this will work:

daniel@daniel-laptop:~/test$ cat Test.java
public class Test {
public static void main(String[] args) {
	float foo = (float) 1.5;
	System.out.println(foo);
}
}

daniel@daniel-laptop:~/test$ javac Test.java
daniel@daniel-laptop:~/test$ java Test
1.5

 

To be honest though, just use double instead of float. Floats and doubles are only approximative data types anyway.

 

Here is a funny example illustrating this:

- load "Math";
> val it = () : unit
- val x = Math.log10 1000.0;
> val x = 3.0 : real
- floor 3.0;
> val it = 3 : int
- floor x;
> val it = 2 : int
- x = 3.0;
> val it = false : bool

 

That is in Standard ML, specifically the MoscowML implementation. The "bug" is that it computes log(1000) to 2.99999999...9 (lots of nines) and rounds it for display, but when I ask it to floor the number, it correctly rounds down to 2.0.

Link to comment
Share on other sites

Ah, I noticed the type casting in your example... when I tried that, it worked (so I stand corrected).

 

To be honest though, just use double instead of float. Floats and doubles are only approximative data types anyway.

 

Yeah, I read that the general tendancy is to use double for decimals (so I won't complicate matters and simple stick with that). Was just thrown off by the float types (which my understanding now is that even with float data types, assigned literals are treated as double types by default - f (or F) treats literals as floats). So much stuff to learn...

 

[ot]Come to think of it, I can only guess that the f part in the value is some form of shorthand typecasting (as opposed to using (float) )?[/ot]

Link to comment
Share on other sites

Hey nrg... coincidentally I've just started my own venture into Java two days ago :P I actually had some previous exposure to it, but this time I want it to be serious :P

 

At least you had some exposure to it.. That must help out somewhat, I'd wager. As for me, I'm venturing in blind. It's like anything else though.. repitition is the mother of all skill.. so I just have to keep at it.. It's already starting to feel more 'natural' (which can't be a bad thing).

 

 

omg now I wanna learn it too let's start a study group??!?!?!?!

 

lol smartass.

 

 

The university decided that I wanted to learn Java, so well....

 

Is it beneficial to your education to learn Java? (I suppose learning new languages never hurts, but I mean given the employment goals afterwards)

 

 

 

Link to comment
Share on other sites

I decided on my own that I wanted to learn Java when on university. But the teacher was so underqualified for this, that I dropped out of this course. The guy actually wanted us to search the web for some ancient (like 1.1 ?) version of Java DK, because he considered it 'better' than newer versions.

Link to comment
Share on other sites

The university decided that I wanted to learn Java, so well....

 

Is it beneficial to your education to learn Java? (I suppose learning new languages never hurts, but I mean given the employment goals afterwards)

 

Well, I don't really think so. As it is right now, I have no aspirations of working in the industry unless I can get something that's more focused on theory. It's the language they chose to use for their "Object Oriented Programming and Design" course so I don't have a choice either way right now.

 

Personally, I don't like Java. It seems way too over-engineered.

 

Want to read lines from a file and write lines to a file?

public ArrayList<String> getLines(String filename) throws FileNotFoundException, IOException{
ArrayList<String> lines = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));

while(reader.ready()) {
	lines.add(reader.readLine());
}

reader.close();

return lines;
}

public void saveLines(Collection<String>, String filename) throws IOException {
File file = new File(filename);
if (!file.exists()) {
	file.createNewFile();
}

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));

for (String s : lines) {
	writer.write(s + "\n");
}

writer.close();
}

 

I suppose it's "good design" or whatever, but it doesn't make it less tedious having to write half a book each time you want to do something.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.