Computer Science Canada

is Java a free? if so were can i get?

Author:  Vertico [ Fri Mar 25, 2005 1:00 pm ]
Post subject:  is Java a free? if so were can i get?

just wondering were it is. i have a java course coming up in school and would like 2 have a copy at home.

Author:  wtd [ Fri Mar 25, 2005 2:50 pm ]
Post subject: 

Yes it's free. http://java.sun.com

Author:  Vertico [ Thu Apr 21, 2005 2:22 pm ]
Post subject: 

okay. i would like to know what i would downloaded for someone who wants to begin/start Java. i went to the website and found alot of different things.

Author:  rizzix [ Thu Apr 21, 2005 2:28 pm ]
Post subject: 

http://java.sun.com/j2se/1.5.0/download.jsp

if ur interested in programming.. always download the JDK...

Author:  Vertico [ Thu Apr 21, 2005 4:18 pm ]
Post subject: 

Okay I downloaded the file that u said, "JDK". After doing so, iv come across most likly the most pathetic, pointless problem.

Dont know how to open it so i can actually put code in. The Java application at my school looks alot like the turing programing screen, not sure if that is what this is suppost to look like but since there is no shortcut on my desktop or a icon in my start up window im having truble finding the program were you type in code.

i can find the java file folder and everything that was installed, but i dont know what im looking for.


P.S. im a 100% noob when it comes to Java.

Author:  wtd [ Thu Apr 21, 2005 4:26 pm ]
Post subject: 

Java source code is just a plain text file that happens to end in ".java". Really. Other than that there's nothing special about it. So, type up a simple "hello world":

code:
import java.io.*;
import java.lang.*;

class HelloWorld
{
   public static void main(String[] args)
   {
      System.out.println("Hello world");
   }
}


Save it as "HelloWorld.java". Open a command prompt (Start -> Accessories). "cd" to the directory where you saved HelloWorld.java. Compile and run like so:

code:
prompt> javac HelloWorld.java

prompt> java HelloWorld

Author:  Vertico [ Thu Apr 21, 2005 7:57 pm ]
Post subject: 

alright so this is what i got out of what you said :

i can make a file anywhere like notepad as long as i save it with a .java extension?

then i open up the Command Promt

thats all i got out of what u said. I dont know what you mean by compile and then what im suppost to do in the Command Prompt window.

remember im brand spanking new to Java and am just tryig to understand how to run programs and were 2 wright them.

Author:  wtd [ Thu Apr 21, 2005 8:12 pm ]
Post subject: 

Yes, you can use any plain text editor. Keep in mind that word-processing programs typically add code you can't see for formatting, and thus are unacceptable. Make sure the extension is ".java". Some text edtors will automatically add ".txt", so you could end up with something like "HelloWorld.java.txt".

Compilation takes source code, which is the stuff that can be read by a human, and turns it into machine code. In this case we take Java source code and compile it into code that's executable by the Java Virtual Machine. We do this with the "javac" program.

We can then tell the Java Virtual Machine to run the code. This is done with the "java" program.

Author:  Vertico [ Fri Apr 22, 2005 2:19 pm ]
Post subject: 

can i get a step by step? i have a file called HelloWorld.java. what do i do after that? dont know how to work command prompt very good.

Author:  rizzix [ Fri Apr 22, 2005 2:27 pm ]
Post subject: 

cd to_directory_with_file_HelloWorld.java
javac HelloWorld.java
java HelloWorld

Author:  Vertico [ Fri Apr 22, 2005 2:45 pm ]
Post subject: 

rizzix wrote:
cd to_directory_with_file_HelloWorld.java


this is the part i still dont understand. how do i "cd" to directory?

Author:  wtd [ Fri Apr 22, 2005 3:23 pm ]
Post subject: 

Vertico wrote:
rizzix wrote:
cd to_directory_with_file_HelloWorld.java


this is the part i still dont understand. how do i "cd" to directory?


"cd" is the command to "change directory". In a filesystem, files are organized into directories (or in the GUI age, "folders"). In the command prompt, you are only ever in one directory at a time. This is typically indicated in the prompt itself.

Of course, directories can themselves contain directories. And in Windows, there can be numerous filesystems, each with their own root. For the C drive, this root looks like:

code:
C:\


For D:

code:
D:\


I'm sure you see the pattern.

A directory at the top level of the C drive named "Foo" looks like:

code:
C:\Foo\


A "Bar" directory inside that one looks like:

code:
C:\Foo\Bar\


See the pattern?

Now, to cd into the "Baz" directory in the "Foo" directory from "C:\Foo\Bar\" I must first move up one level:

code:
C:\Foo\Bar\> cd ..


Then, I need to change into the "Baz" directory:

code:
C:\Foo\> cd Baz


Of course, this could have been shortened to:

code:
C:\Foo\Bar\> cd ..\Baz

Author:  Vertico [ Fri Apr 22, 2005 4:31 pm ]
Post subject: 

okay i found out why i was having so much trouble with the changing directory. For some reason it wont change to my D drive where i use to have all the Java stuff. i moved it to my C drive and it now will change. Also i ran the helloworld file and im pretty sure it worked since it is only suppost to say hello world and thats what it said.



but then i tryed to run a demo that came with it (TicTacToe) and repeated the same actions i took for the helloworld file and changed my directory to it. then comacted with javac and then when i did the java TicTacToe all it said was TicTacToe and didnt actauly do anything else.

did i do something wrong? or is that all its suppost to do/say?

Author:  wtd [ Fri Apr 22, 2005 5:45 pm ]
Post subject: 

To change to a different drive (C, D, etc.):

code:
C:\Yada\Yada\Yada\> D:

Author:  Vertico [ Fri Apr 22, 2005 9:42 pm ]
Post subject: 

alright so lets see if i can understand this.


so i changed the directory to :

code:
C:\Program FIles\Java


and the file called Helloworld.java is saved in the file Java. then i type

code:
prompt> javac Helloworld.java

then
code:
prompt>java Helloworld


is this right??

after that it should show me what is suppost to be printed off right?
in the command prompt screen or in a new window?

well when i do all that all it does is writes Helloworld in the command prompt screen. Even if the msg inside the code is "happy happy".

What am i doing wrong and what should i be doing?

Author:  wtd [ Fri Apr 22, 2005 9:57 pm ]
Post subject: 

Yes, anything printed with System.out goes to the standard output, which is the terminal window, by default.

Can you please show me the exact program you're compiling?

Author:  Vertico [ Fri Apr 22, 2005 10:21 pm ]
Post subject: 

code:

import java.io.*;
import java.lang.*;

class HelloWorld
{
   public static void main(String[] args)
   {
      System.out.println("what is up people");
   }
}




its the exact program u wrote out before. except i changed the print

Author:  wtd [ Fri Apr 22, 2005 10:34 pm ]
Post subject: 

After you made a change to the program, did you recompile it, or just run it again?

When you make a change to a Java program you must recompile it. You see, the source code lives in (in this case) HelloWorld.java. But when we compile with:

code:
javac HelloWorld.java


The program is converted to a format the Java virtual machine can use, and this is placed in the file HelloWorld.class. It's this file which:

code:
java HelloWorld


Is concerned with. If you don't recompile, that file is unchanged, and any changes to HelloWorld.java won't be seen in the results.

Author:  Vertico [ Sat Apr 23, 2005 8:14 am ]
Post subject: 

do u know of a website that can help me through this? like the most slowest newbiest yet effective site to help me learn how to run files?

cuz i keep trying what ur saying and i keep getting nothing back but what i type.

Author:  Hikaru79 [ Sun Apr 24, 2005 1:45 pm ]
Post subject: 

Also, you don't need either of the import statements you used at the top. You don't need java.io.* because you're not using it anywhere in your program, and you don't need java.lang.* because its imported by default -- you don't need to worry about that.

Author:  wtd [ Sun Apr 24, 2005 4:12 pm ]
Post subject: 

Hikaru79 wrote:
Also, you don't need either of the import statements you used at the top. You don't need java.io.* because you're not using it anywhere in your program


It never hurts to have either of these import statements.

Author:  1of42 [ Sun Apr 24, 2005 9:33 pm ]
Post subject: 

But isn't it extraneous and unnecessary?

Author:  rizzix [ Sun Apr 24, 2005 9:48 pm ]
Post subject: 

one can argue.. it self-documents the code.. so yea... well refering to the java.lang package i.e.. the java.io is unnecessary.

Author:  Hikaru79 [ Wed Apr 27, 2005 12:00 am ]
Post subject: 

wtd wrote:
Hikaru79 wrote:
Also, you don't need either of the import statements you used at the top. You don't need java.io.* because you're not using it anywhere in your program


It never hurts to have either of these import statements.


Really? It doesn't hurt to import packages you don't use at all? All this time I've been doing stuff like
Java:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;

if those would be the only classes I was using in a particular class. I figured I was getting at least a tiny bit of a boost here for only importing exactly what I needed as opposed to
Java:
import java.io.*;
. Have I been misinformed this whole time? Or is there some sort of benefit to my stinginess, however small it may be? Or is the compiler smart enough only to load the bare neccesities?

Author:  rizzix [ Wed Apr 27, 2005 12:16 am ]
Post subject: 

no this is a compile time issue.. not a runtime issue.. the compiler will optimise ur code importing only what you need.. actually imports are all syntatic sugar.. the bytecode uses the full form internally.

but yea.. ur stingyness can reduce compile time.. but not necessarily improve runtime speed... on the other hand it can be seen as self-documentation Wink

Author:  Hikaru79 [ Wed Apr 27, 2005 12:40 am ]
Post subject: 

rizzix wrote:
no this is a compile time issue.. not a runtime issue.. the compiler will optimise ur code importing only what you need.. actually imports are all syntatic sugar.. the bytecode uses the full form internally.

but yea.. ur stingyness can reduce compile time.. but not necessarily improve runtime speed... on the other hand it can be seen as self-documentation Wink

*phew* At least I have *some* justification Laughing Thanks, rizzix! Wink

Author:  rizzix [ Wed Apr 27, 2005 2:13 pm ]
Post subject: 

yes but there is no point in trying to import stuff you dont use.. its very bad practise... dont do it.

Author:  wtd [ Wed Apr 27, 2005 3:53 pm ]
Post subject: 

Don't micro-optimize. If you want several classes from "java.io", import "java.io.*". You could spend lots of valuable time individually importing each class you use, but at the end you'd have done nothing the compiler/VM couldn't do in a fraction of a second.

Author:  rizzix [ Wed Apr 27, 2005 4:08 pm ]
Post subject: 

depends... if you are using a tool like eclipse... they can automaticaly organise and fold ur import statements.. and by organising i mean expanding it completly and grouping/sorting them in a perticular order..

it actually makes, looking up classes used in that class or file, a breeze.. and bsides cuz of the folding, it dosen't really come in way your while coding..

as for the time "wasted" well, actually none of it is actually wasted.. its all done automatically.

Author:  wtd [ Wed Apr 27, 2005 4:37 pm ]
Post subject: 

Perhaps, but I mean, don't spend time typing out each class name you're importing if a "import javax.swing.*;" or such will do the trick.

Author:  Hikaru79 [ Wed Apr 27, 2005 5:43 pm ]
Post subject: 

wtd wrote:
Perhaps, but I mean, don't spend time typing out each class name you're importing if a "import javax.swing.*;" or such will do the trick.

Aaah... yet another case of me wasting two minutes of coding time to save two milliseconds of compiling time Confused I gotta stop doing that.


: