Introduction to Java

  • Thread starter JasonRox
  • Start date
  • #1
JasonRox
Homework Helper
Gold Member
2,287
3

Main Question or Discussion Point

Alright, I did the whole Hello World! thing, but I got stuck on one part.

I downloaded the Sun.Java thing, but now when I go into the command prompt and to try and compile it... the computer does not recognize the "javac" command.

What's going on? Is there another program?
 

Answers and Replies

  • #2
chroot
Staff Emeritus
Science Advisor
Gold Member
10,165
34
Make sure the javac program is in your PATH environment variable. It's usually in c:\j2sdk1.4.2\bin\, for example.

Right click on My Computer, hit Properties, Advanced, Envrionment Variables, and add this directory to the PATH.

- Warren
 
  • #3
436
1
Before you can use the javac command you need to set the path with the path command. Depending on the folder where you jsdk is installed you can type something like this in the command line:
Code:
path "C:\Program Files\j2sdk_nb\j2sdk1.4.2\bin"
and press Enter.
 
  • #4
JasonRox
Homework Helper
Gold Member
2,287
3
A simple question about one of the Java exercises they have.

Why is my 2/3 coming out as zero?

Here is the exact line of declaration:

double y = 2/3;

It doesn't get any easier than that. I tried with brackets around it too. I also tried 1/3 + 1/3, with brackets.

The only way it seems to work is like this...

double x=2;
double y=x/3;

This is not efficient at all, nor practical when I go into loops.

What's the problem?
 
  • #5
690
1
Because 2 and 3 are integers. You have to explicitly cast these as doubles or use 2.0 and/or 3.0.

Code:
import java.util.*;

public class test {

    public static void main (String args[]) {
        double y = 2.0/3;
        System.out.println(y);
    }
}
[edit] Ok, here's what's going on. Java reads the code on the right of the equal sign and performs whatever task is required. Java then yakes the result and stores that in the desired format of the variable. In your case, you are dividing an integer 2 by the integer three. 3 doesn't go evenly into 2 so the result is zero. 0 is converted to a double(0.0) and then stored into your variable y. The above code is one way around this or you can do the following:

Code:
import java.util.*;

public class test {

    public static void main (String args[]) {
        double y = (double)2/3;
        System.out.println(y);
    }
}
Programming languages are not very smart. They don't see the left and right sides at the same time as we do. The see things as a series of steps. If you start thinking in steps then your code will come a little easier.
 
Last edited:
  • #6
JasonRox
Homework Helper
Gold Member
2,287
3
Thanks!

I appreciate it.
 
  • #7
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,843
17
Programming languages are not very smart. They don't see the left and right sides at the same time as we do. The see things as a series of steps. If you start thinking in steps then your code will come a little easier.
So the poster doesn't get the wrong impression, this fact is often a good thing. While it would be nice for the compiler to figure out what we really meant, it would be extraordinarily annoying when the compiler guesses wrong.
 

Related Threads for: Introduction to Java

  • Last Post
Replies
10
Views
1K
  • Last Post
Replies
4
Views
7K
  • Last Post
Replies
4
Views
3K
  • Last Post
Replies
6
Views
28K
  • Last Post
Replies
22
Views
4K
  • Last Post
6
Replies
129
Views
21K
  • Last Post
Replies
13
Views
6K
  • Last Post
Replies
4
Views
2K
Top