wood burning stoves*
The moose likes Beginning Java and the fly likes could someone explain the basics from below attached code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "could someone explain the basics from below attached code" Watch "could someone explain the basics from below attached code" New topic
Author

could someone explain the basics from below attached code

johnson james
Greenhorn

Joined: Feb 12, 2009
Posts: 4
What are each line of code stand for also each syntax? eg: why we need to write "import java.io.*? I would really appreciate if someone can help with this. Thank you in advance!
=============================

[edit]Add code tags. CR[/edit]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

First, welcome to the javaranch. Glad you came by!

You may want to read some of this. It gives you a good idea how to get the most from this site.

Many folks are going to read your post and not bother. It appears to the reader like you are asking us to do your work for you - i.e. that you've put NO effort at all into figuring things out.

If you have specific questions, then by all mean, ask them, but a blanket post of "what does all this mean" will turn many folks off from helping you.

Now, to answer your specific question... the first line is called an import statement. It's telling java that you are going to use some special, pre-written classes, and where to find them. Java will use that when it parses your code. If it is not obvious where the class is, it will basically look in the directories named in the various import statements (you can have more than one import).

You can write your code without ever using imports, but then you'd have to refer to some classes by really long names. using the import saves some typing.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
johnson james
Greenhorn

Joined: Feb 12, 2009
Posts: 4
fred rosenberger wrote:First, welcome to the javaranch. Glad you came by!

You may want to read some of this. It gives you a good idea how to get the most from this site.

Many folks are going to read your post and not bother. It appears to the reader like you are asking us to do your work for you - i.e. that you've put NO effort at all into figuring things out.

If you have specific questions, then by all mean, ask them, but a blanket post of "what does all this mean" will turn many folks off from helping you.

Now, to answer your specific question... the first line is called an import statement. It's telling java that you are going to use some special, pre-written classes, and where to find them. Java will use that when it parses your code. If it is not obvious where the class is, it will basically look in the directories named in the various import statements (you can have more than one import).

You can write your code without ever using imports, but then you'd have to refer to some classes by really long names. using the import saves some typing.
Thanks for the quick reply
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
One of the hints Fred gave you tells you to use the code tags. I have added them to your first post; they preserve indentation and you can see how much better it looks. There are details of imports in the Java Language Specification, but it is by no means an easy read!
johnson james
Greenhorn

Joined: Feb 12, 2009
Posts: 4
Thank you Campbell for your help!

Another question I have is when to use curly brackets? Does the position of curly brackets matters?
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Do you learn java? or you are asking on the fly? Is this your doubt after reading basic java stuff?
johnson james
Greenhorn

Joined: Feb 12, 2009
Posts: 4
Vishal, thanks for your quick reply. I started learning basics of java by myself and searching for all available help. I cant find any help explaining the rules of using curly brackets. I would appreciate any help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Braces {} divide the code into "blocks." Some blocks are compulsory: for example the bodyof a class and the body of a method each live inside a block.
Some blocks are optional. For example the compiler will happily take a single line after if () but if you want two lines included in the "if" you must put them in a block with {}. It is probably still better to use {} there regardless.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

if by 'position' you mean 'does it matter if i do one or the other of these:



The answer is "yes it does matter - but not to java". Bracket position is one of the most hotly debated topics in all of computer science. People have stronger beliefs about this than they do about the middle east crisis, the economic stimulus package, or if Kirk or Picard is the better captain of the Enterprise.

The truth is, most places will have some coding standard that will tell you their preferred way, and you should follow it. Otherwise, do whatever you like best.
Pankaj Rathod
Greenhorn

Joined: Feb 02, 2009
Posts: 7
Johnson,

Its nice that you are learing Java.

But I think (and guessing from your original question) that the code you provided is not the best way to get started in Java. You need to start with a pretty basic program (like the traditional HelloJava/HelloWorld) and then build up by learing concepts one by one.

You can start with Java Tutorial on java.sun.com.

Hope I am not being preachy.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: could someone explain the basics from below attached code
 
Similar Threads
Kindly help me
Problem in file transfer
primality formula
PrintWriter please help
validation specifications for sorting date,integer,string...