# Introduction to Java

#### JasonRox

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?

Related Computing and Technology News on Phys.org

#### chroot

Staff Emeritus
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

#### ramollari

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.

#### JasonRox

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?

#### faust9

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);
}
}
 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:

Homework Helper
Gold Member
Thanks!

I appreciate it.

Staff Emeritus