India Broadband Forum


Java Programming : Doubts

This is a discussion on Java Programming : Doubts within the Windows forums, part of the Operating Systems category; 1. What is the purpose of using Strings [] args in the following statement, considering that no value is being ...

Go Back   India Broadband Forum > Computers > Operating Systems > Windows

India Broadband Forum


                      

Reply

 

LinkBack Thread Tools Display Modes
Old 08-03-09, 10:19 PM   #1
newprouser
Guest
 
Posts: n/a
Default Java Programming : Doubts

1.

What is the purpose of using Strings [] args in the following statement, considering that no value is being returned to the main function.

public static void main (String[] args )

and then string [] means a string is being returned, so why the statement args, that too without being separated by comma ?


2. is it possible to have the following for loop like in C/C++


for ( ; condition ; )
{ ..
.. };
  Reply With Quote
Old 08-03-09, 10:46 PM   #2
Gold Member
 
nandini's Avatar
 
Join Date: Jun 2009
Location: Pune
Posts: 487
Rep Power: 3
nandini is a jewel in the roughnandini is a jewel in the roughnandini is a jewel in the rough
Default

Quote:
Originally Posted by newprouser View Post
1.

What is the purpose of using Strings [] args in the following statement, considering that no value is being returned to the main function.

public static void main (String[] args )

and then string [] means a string is being returned, so why the statement args, that too without being separated by comma ?


2. is it possible to have the following for loop like in C/C++


for ( ; condition ; )
{ ..
.. };


1. public static void main(String[] args) is the method from where your java application begins.
We can have multiple main method but the signature exactly matching to the above method, is recognised by jvm and it starts by calling that main method.
It's like entry point.

The purpose of using String[] args is that, you can provide inputs to your application before actually running it.

String[] args means array of String. Hope you know what array is.

2. Better to check for yourself. In my opinion it should run.
nandini is offline   Reply With Quote
Old 08-03-09, 10:50 PM   #3
Jedi knight
 
Luke Skywalker's Avatar
 
Join Date: Jul 2009
Location: IBF
Age: 21
Posts: 1,816
Rep Power: 5
Luke Skywalker is a jewel in the roughLuke Skywalker is a jewel in the roughLuke Skywalker is a jewel in the roughLuke Skywalker is a jewel in the rough
Default

String args[] also.
String... args also. This is called var args.
What nandini had said is right? rep+ to her for beating me to answer.
Luke Skywalker is offline   Reply With Quote
Old 08-03-09, 11:18 PM   #4
newprouser
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by nandini View Post
1. public static void main(String[] args) is the method from where your java application begins.
We can have multiple main method but the signature exactly matching to the above method, is recognised by jvm and it starts by calling that main method.
It's like entry point.

The purpose of using String[] args is that, you can provide inputs to your application before actually running it.

String[] args means array of String. Hope you know what array is.
well I have used C/C++ and OOPS in it too.

are you talking about operator overloading in the first para ??


Quote:
2. Better to check for yourself. In my opinion it should run.
not running. thats why i was wondering if there's any other alternative statement for that.
  Reply With Quote
Old 08-03-09, 11:24 PM   #5
Platinum Member
 
Join Date: Sep 2008
Posts: 3,140
Rep Power: 10
Punch Farce is a name known to allPunch Farce is a name known to allPunch Farce is a name known to allPunch Farce is a name known to allPunch Farce is a name known to allPunch Farce is a name known to all
Default

NPU,

that's the equivalent of main (argc, argv) in C and C++

"String [] args" is the same as "char **argv" or "char *argv[]" for
all practical purposes.

they are the command line parameters when you run the program.

-F

what exactly is not working in your program?
Punch Farce is offline   Reply With Quote
Old 08-03-09, 11:24 PM   #6
Gold Member
 
nandini's Avatar
 
Join Date: Jun 2009
Location: Pune
Posts: 487
Rep Power: 3
nandini is a jewel in the roughnandini is a jewel in the roughnandini is a jewel in the rough
Default

1. It's not operator overloading. in fact java doesn't support operator overloading.

you can declare an array of String in two ways.

String[] arr=null; //more readable
String arr[]=null;

2.I think in for loop initialization is a must. So you can write like
for(int i=0;condition; ) //it should work.
by the way why the hell do you want to write it? Do you want to create infinite loop?

PS: Farce is right,as usual.
nandini is offline   Reply With Quote
Old 08-03-09, 11:29 PM   #7
newprouser
Guest
 
Posts: n/a
Default

Sorry can't rep now, will rep ya ASAIC
  Reply With Quote
Old 08-03-09, 11:31 PM   #8
Gold Member
 
nandini's Avatar
 
Join Date: Jun 2009
Location: Pune
Posts: 487
Rep Power: 3
nandini is a jewel in the roughnandini is a jewel in the roughnandini is a jewel in the rough
Default

Quote:
Originally Posted by newprouser View Post
Sorry can't rep now, will rep ya ASAIC
It doesn't matter anything to me. What really important is that whether you got your doubt cleared.
nandini is offline   Reply With Quote
Old 08-03-09, 11:33 PM   #9
Platinum Member
 
Join Date: Sep 2008
Posts: 3,140
Rep Power: 10
Punch Farce is a name known to allPunch Farce is a name known to allPunch Farce is a name known to allPunch Farce is a name known to allPunch Farce is a name known to allPunch Farce is a name known to all
Default

Quote:
Originally Posted by nandini View Post
1by the way why the hell do you want to write it? Do you want to create infinite loop?
break;
goto
return
exit

;-)

and what's wrong with infinite loops?
this is the program that NPU is writing

login to IBF
for ( ;forever; ) {
post
}


Quote:
Originally Posted by nandini View Post
PS: Farce is right,as usual.
my online reputation is much better than my domestic one ;-)
I wish you could tell that to my wife ;-)
Punch Farce is offline   Reply With Quote
Old 08-03-09, 11:43 PM   #10
newprouser
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by farce View Post
NPU,

that's the equivalent of main (argc, argv) in C and C++

"String [] args" is the same as "char **argv" or "char *argv[]" for
all practical purposes.

they are the command line parameters when you run the program.

-F
Quote:
Originally Posted by nandini View Post
1. It's not operator overloading. in fact java doesn't support operator overloading.

you can declare an array of String in two ways.

String[] arr=null; //more readable
String arr[]=null;

2.I think in for loop initialization is a must. So you can write like
for(int i=0;condition; ) //it should work.
by the way why the hell do you want to write it? Do you want to create infinite loop?

PS: Farce is right,as usual.
thanks both of you , got the first one cleared.


Quote:
Originally Posted by farce View Post
what exactly is not working in your program?
well i'm not able to create a loop like this in java, atho it will work in C

for ((blank) ; condition ;(blank) )

Quote:
by the way why the hell do you want to write it? Do you want to create infinite loop?
even god won't know why i'm doing what i'm doing

Quote:
Originally Posted by farce View Post
break;
goto
return
exit

;-)

and what's wrong with infinite loops?
this is the program that NPU is writing

login to IBF
for ( ;forever; ) {
post
}



my online reputation is much better than my domestic one ;-)
I wish you could tell that to my wife ;-)


nice idea farce, maybe i should use Fx and use a macro with it

---



Quote:
Originally Posted by farce View Post
my online reputation is much better than my domestic one ;-)
I wish you could tell that to my wife ;-)
i'd guess her reply to be "the boss is always right "

Last edited by newprouser; 08-03-09 at 11:43 PM. Reason: Automerged Doublepost
  Reply With Quote
Old 08-04-09, 12:07 AM   #11
Jedi knight
 
Luke Skywalker's Avatar
 
Join Date: Jul 2009
Location: IBF
Age: 21
Posts: 1,816
Rep Power: 5
Luke Skywalker is a jewel in the roughLuke Skywalker is a jewel in the roughLuke Skywalker is a jewel in the roughLuke Skywalker is a jewel in the rough
Default

It is working for me:-
class Main {

public static void main(String[] args)
{ int i=1 ;
for ( ;i >0 ; )
{
i=1;
}
}
}
Luke Skywalker is offline   Reply With Quote
Old 08-04-09, 12:12 AM   #12
newprouser
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by Luke Skywalker View Post
It is working for me:-
class Main {

public static void main(String[] args)
{ int i=1 ;
for ( ;i >0 ; )
{
i=1;
}
}
}
My bad, i had initialized ch1 as zero, so that was the problem.

Quote:
public class chaz
{
public static void main (String[] args )
{
char ch1;

ch1=1;

for ( ; ch1 < 127 ; )
{
System.out.println(ch1);
ch1++;
}

}
}
  Reply With Quote
Old 08-04-09, 12:15 AM   #13
Jedi knight
 
Luke Skywalker's Avatar
 
Join Date: Jul 2009
Location: IBF
Age: 21
Posts: 1,816
Rep Power: 5
Luke Skywalker is a jewel in the roughLuke Skywalker is a jewel in the roughLuke Skywalker is a jewel in the roughLuke Skywalker is a jewel in the rough
Default

That is also a valid program but it print character upto 127
Luke Skywalker is offline   Reply With Quote
Old 08-04-09, 12:17 AM   #14
newprouser
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by Luke Skywalker View Post
That is also a valid program but it print character upto 127
yes, i was trying to print ASCII values, since its from 0-127, i initialized ch1 as 0., which seems to be the cause of the problem.
  Reply With Quote
Old 08-04-09, 12:20 AM   #15
Jedi knight
 
Luke Skywalker's Avatar
 
Join Date: Jul 2009
Location: IBF
Age: 21
Posts: 1,816
Rep Power: 5
Luke Skywalker is a jewel in the roughLuke Skywalker is a jewel in the roughLuke Skywalker is a jewel in the roughLuke Skywalker is a jewel in the rough
Default

Post the exception & error you get. They are your best teachers.
Luke Skywalker is offline   Reply With Quote
Old 08-04-09, 12:26 AM   #16
newprouser
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by Luke Skywalker View Post
Post the exception & error you get. They are your best teachers.
well well it was for a reason u guys asked me not to use an ide in the beginning, coz the original program (with ch1=0) is working fine in CMD, but in IDE , no output was displayed (except process completed).
  Reply With Quote
Old 08-04-09, 12:30 AM   #17
Jedi knight
 
Luke Skywalker's Avatar
 
Join Date: Jul 2009
Location: IBF
Age: 21
Posts: 1,816
Rep Power: 5
Luke Skywalker is a jewel in the roughLuke Skywalker is a jewel in the roughLuke Skywalker is a jewel in the roughLuke Skywalker is a jewel in the rough
Default

Yes it will work. Don't use IDE now. Use cmd only.
Luke Skywalker is offline   Reply With Quote
Old 08-04-09, 12:31 AM   #18
Gold Member
 
nandini's Avatar
 
Join Date: Jun 2009
Location: Pune
Posts: 487
Rep Power: 3
nandini is a jewel in the roughnandini is a jewel in the roughnandini is a jewel in the rough
Default

Quote:
Originally Posted by newprouser View Post
well well it was for a reason u guys asked me not to use an ide in the beginning, coz the original program (with ch1=0) is working fine in CMD, but in IDE , no output was displayed (except process completed).
it can't be.
post your code.
nandini is offline   Reply With Quote
Old 08-04-09, 12:37 AM   #19
newprouser
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by nandini View Post
it can't be.
post your code.
Quote:
public class chaz
{
public static void main (String[] args )
{
char ch1;

ch1=0;

for (;ch1<127; )
{
System.out.println(ch1);
ch1++;
}

}
}
I use Jcreator Pro IDE. v4.5
  Reply With Quote
Old 08-04-09, 10:41 PM   #20
Bronze Member
 
matrix's Avatar
 
Join Date: Dec 2008
Location: Kalyan West
Posts: 254
Rep Power: 1
matrix is on a distinguished road
Default

Simple suggestion. Can't this be written as follows:

public class chaz
{
public static void main (String[] args )
{
for (ch1=1;ch1<127; ch1++)
{
System.out.println(ch1);
}

}
}

This will be proper and clear way of coding as per the standard followed by many java developers.
matrix is offline   Reply With Quote
Reply

Tags
doubts, java, programming

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Vote: would you like to have a programming section in IBF ? newprouser Suggestions and Complaints 30 08-30-09 12:56 AM
Learning Java ~ What are the best choices of tools ? newprouser Windows 42 08-01-09 08:05 PM
Installling java saurabh2446 Nokia 1 06-23-09 12:44 PM
Any how to videos for Flash Programming? vishnu54 Windows 0 05-06-09 05:28 PM


All times are GMT +5.5. The time now is 12:48 PM.


India Broadband Forum