• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

could someone explain the basics from below attached code

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic