...

View Full Version : Java Basics



jpnv8
03-25-2008, 01:00 AM
import java.io.*;//Ask what for or when to use

public class SAMPLE //Ask what for when to use
{

public static InputStreamReader reader = new InputStreamReader(System.in); //for input/output
public static BufferedReader input = new BufferedReader(reader); //for input/output
public static void main (String [] args) //constant statement in java what for?
throws Exception{ //for num1=Integer.parseInt(input.readLine());
hey whats up! hi im new to java. can you please help me understand what these codes are used for.

A.)
public class SAMPLE
what does public class mean?

All i know is the "SAMPLE" statement. It is the filename of the program and it CASE SENSITIVE

B.)
import java.io.*;
I heard this was a library? i also heard aboutjava.util, java.swing. Can you please explain to me the use of these libraries. Is there a website containing all possible libraries in java which shows their complete function.

C.)
throws Exception
Whats this code for? when is it used?

D.)
public static void main (String [] args)
I heard this is a constant statement in java. Can someone please tell me whats this statement for

E.)
public static InputStreamReader reader = new InputStreamReader(System.in); //for input/output
public static BufferedReader input = new BufferedReader(reader); //for input/output

Someone told me these codes are used if you have an input/output
in your program. Can someone explain word for word the purpose of these codes

THANKS in advance =)
by the way this is kind of silly should i call these block of sentences CODES OR A STATEMENT

Kor
03-25-2008, 09:25 AM
I'll move the thread to the JAVA Forum

Aradon
03-25-2008, 06:48 PM
import java.io.*;//Ask what for or when to use

public class SAMPLE //Ask what for when to use
{

public static InputStreamReader reader = new InputStreamReader(System.in); //for input/output
public static BufferedReader input = new BufferedReader(reader); //for input/output
public static void main (String [] args) //constant statement in java what for?
throws Exception{ //for num1=Integer.parseInt(input.readLine());
hey whats up! hi im new to java. can you please help me understand what these codes are used for.

A.)
public class SAMPLE
what does public class mean?

All i know is the "SAMPLE" statement. It is the filename of the program and it CASE SENSITIVE

B.)
import java.io.*;
I heard this was a library? i also heard aboutjava.util, java.swing. Can you please explain to me the use of these libraries. Is there a website containing all possible libraries in java which shows their complete function.

C.)
throws Exception
Whats this code for? when is it used?

D.)
public static void main (String [] args)
I heard this is a constant statement in java. Can someone please tell me whats this statement for

E.)
public static InputStreamReader reader = new InputStreamReader(System.in); //for input/output
public static BufferedReader input = new BufferedReader(reader); //for input/output

Someone told me these codes are used if you have an input/output
in your program. Can someone explain word for word the purpose of these codes

THANKS in advance =)
by the way this is kind of silly should i call these block of sentences CODES OR A STATEMENT

After I'm done explaining most of this, I suggest that you look at this official java tutorial:

Sun Tutorial (http://java.sun.com/docs/books/tutorial/)

Now for your questions

A) public class is the beginning of any class in Java. public means that the class is public to anybody. So users can use the class and see it outside of the class statement. class is just stating that we are declaring a class

B) http://java.sun.com/javase/6/docs/api/

That is the API used for java. It contains all java libraries and classes that one can use from the default libraries.

java.io.* means that we are importing from standard java, in the io package, and everything inside of it.

C) Throws Exception is a way to tell the program that it is possible that this class / method will throw an exception. Typically this way is frowned upon as it make more sense to use a try catch block to catch exceptions and handle them in the way you want. For more information, see the tutorial I pointed to.

D) This statement is used to tell java where the main part of our program is. That is, what code java should execute first.

E)
public static InputStreamReader reader = new InputStreamReader(System.in); //for input/output
public static BufferedReader input = new BufferedReader(reader); //for input/output

Sure! What's going on is the first thing you do is create a streamreader with an input device of System.in.

typically System.in is the keyboard, but there are many different types of streams that can be inputted into here. However, if we want keyboard input, System.in is what we want!

We want an InputStreamReader because we need it to be a stream that we can read input from.

We then place this reader into a BufferedReader in order to allow for some type of easy interface with the stream. Once again, more details can be read in the tutorial.

jpnv8
03-27-2008, 01:42 AM
thx bro:)

jpnv8
04-02-2008, 10:13 AM
num1= Float.parseFloat(input.readLine());
//num1 --->(the variable)
//Integer.parseInt---> (data type)
//Double.parseDouble , Character.parsechar (other data types)
//(input.readLine()); /**ask what for**/


what does (input.readLine()) and parse, means? parseInt?

thanks in advance

sobrien79
04-02-2008, 04:48 PM
parse takes one thing and attempts to turn it into another. Each of the number types has a parse method to attempt to turn a string in a number. They will throw a NumberFormatException if they fail.

Integer.parseInt
Float.parseFloat
Long.parseLong
Double.parseDouble

Consider:


int i1 = Integer.parseInt("1234"); //i1 now equals 1234
int i2 = Integer.parseInt("1234.45"); //throw NumberFormatException because integers cannot contain decimals

double d1 = Double.parseDouble("1234"); //d1 now equals 1234.0
double d2 = Double.parseDouble("1234.45"); //d2 now equals 1234.45

Aradon
04-02-2008, 04:58 PM
As a last note

input.readLine() just takes the BufferedReader that we created through Standard In (typically the keyboard) and grabs a line from it.

So in our case, it would be user input. The return is a string, thus why it needs to be parsed into it's appropriate function as exampled above this post by sobrien79.

jpnv8
04-03-2008, 03:32 AM
parse takes one thing and attempts to turn it into another. Each of the number types has a parse method to attempt to turn a string in a number. They will throw a NumberFormatException if they fail.

Integer.parseInt
Float.parseFloat
Long.parseLong
Double.parseDouble

Consider:


int i1 = Integer.parseInt("1234"); //i1 now equals 1234
int i2 = Integer.parseInt("1234.45"); //throw NumberFormatException because integers cannot contain decimals

double d1 = Double.parseDouble("1234"); //d1 now equals 1234.0
double d2 = Double.parseDouble("1234.45"); //d2 now equals 1234.45



so (input.readLine()); is the command to store the 1234?

sobrien79
04-03-2008, 04:29 PM
input.readLine() says to the user, give me something to work with. Then the user enters in something and the program goes on from there.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum