LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2006, 11:39 AM   #1
spank
Member
 
Registered: Aug 2003
Location: Romania
Distribution: Ubuntu 6.06
Posts: 278

Rep: Reputation: 30
Java programing my first steps


I'm just begining to program in java using eclipse. so go easy on me.

One question is how can i use the sqrt function ?
I'm thinking that I have to import something like some libraries.
My only programing experience is in C so if you can make some analogies with that it will sure help.
Thank you!

Some external java resources links will be great.
 
Old 10-06-2006, 12:01 PM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
In Java, the kind of equivalent of "#include <header>" is an import statement. The syntax is as follows:

import package;

There is one package that you don't need to include and that's java.lang, which is included automatically.

In java.lang, you have a class called Math. I haven't done much C, but structs are kind of like classes (at least, C++ classes are like structs in C). The square root function, sqrt(double x) is a static method of that class. What that basically means is you don't need to use that function on a particular instance of that class - it's ok to use the class name to call the function.

Let me show you a quick example of using the square root function:

Code:
// Do we need "import java.lang" here? No, because
// it's included by default.

public class Test
{
  public static void main(String[] args)
  {
    double x = 5;
    double sqrtX = Math.sqrt(5);
  }
}
Does that help at all? I've not touched Java for a while actually and I'm surprised I'm able to remember this stuff!
 
Old 10-06-2006, 12:05 PM   #3
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
int sqVal = Math.sqrt(val);

you dont have to import math.. but when you need to import something use the organize imports in eclipse.. on the right click menu or keyboard shortcut ctrl+shift+o

hth

edit> im always a little slow i guess..
 
Old 10-06-2006, 03:29 PM   #4
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Quote:
Originally Posted by Nylex
The square root function, sqrt(double x) is a static method of that class. What that basically means is you don't need to use that function on a particular instance of that class - it's ok to use the class name to call the function.
There's no such thing as java.lang.Math instances. (This is enforced my making the constructor private.) It's just a namespace for static methods, which is a class only because Java requires everything to be in a class.
 
Old 10-06-2006, 03:32 PM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Ah OK. Haven't done Java for a while, so have forgotten some things. Generally that's true though isn't it, when you have static methods, you don't need an instance of the class. I think that's the same in C++.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
programing in java on linux phoenix_wolf Linux - Newbie 21 01-26-2005 06:35 AM
Easy steps to Java plug-in installation RaiReg Mandriva 5 01-26-2004 11:18 PM
Java Programing Tutorial embalmedlenin Programming 2 08-23-2003 09:53 AM
Java programing VincentB Linux - Software 3 07-14-2003 07:17 PM
About Java Sound programing figadiablo Programming 4 03-20-2003 10:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration