File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

could someone explain the basics from below attached code

 
johnson james
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
johnson james
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1374
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you learn java? or you are asking on the fly? Is this your doubt after reading basic java stuff?
 
johnson james
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic