Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Introduction to Java

  1. Mar 31, 2005 #1

    JasonRox

    User Avatar
    Homework Helper
    Gold Member

    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?
     
  2. jcsd
  3. Mar 31, 2005 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  4. Mar 31, 2005 #3
    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 (Text):
    path "C:\Program Files\j2sdk_nb\j2sdk1.4.2\bin"
    and press Enter.
     
  5. Apr 2, 2005 #4

    JasonRox

    User Avatar
    Homework Helper
    Gold Member

    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?
     
  6. Apr 2, 2005 #5
    Because 2 and 3 are integers. You have to explicitly cast these as doubles or use 2.0 and/or 3.0.

    Code (Text):

    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 (Text):

    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: Apr 2, 2005
  7. Apr 2, 2005 #6

    JasonRox

    User Avatar
    Homework Helper
    Gold Member

    Thanks!

    I appreciate it.
     
  8. Apr 3, 2005 #7

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Introduction to Java
  1. Java's Scanner (Replies: 6)

  2. Java Help (Replies: 7)

  3. Java help (Replies: 15)

  4. Java q (Replies: 3)

  5. Java problem. (Replies: 4)

Loading...