aspose file tools*
The moose likes Beginning Java and the fly likes doubt in some programs and question in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

doubt in some programs and question in java

Will Zelan
Ranch Hand

Joined: Apr 15, 2012
Posts: 56

Jesper de Jong wrote:
Will Zelan wrote:
Jesper de Jong wrote:
Will Zelan wrote:i had doubt in 14 and 22 nd line,i know it is the method which do some functions,in 14th line what are passing and 22nd line what is inta[] and int n can you tell me

What do you think the answer to that question is yourself?

passing the parameter as array and array length in 14th line,will you tell me

Exactly, it's just a method call like any other method call, where parameters are passed. Nothing special going on.

why passing array.length in 14th line and what about 22nd line int [] and int n
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Will Zelan wrote:why passing array.length in 14th line


Look at the method being called to see how it's used.

and what about 22nd line int [] and int n


What about it?
Will Zelan
Ranch Hand

Joined: Apr 15, 2012
Posts: 56

Jeff Verdegan wrote:
Will Zelan wrote:why passing array.length in 14th line


Look at the method being called to see how it's used.

and what about 22nd line int [] and int n


What about it?


passing parameter to method i understand, so we are passing array,is it need to pass arraylength also?

previously we passed parameter in method and what about above int a[] and int n which represents in the code,iam confused
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Will Zelan wrote:
passing parameter to method i understand, so we are passing array,is it need to pass arraylength also?


Look at how the method uses that parameter. Work through what it would do if that parameter were less than the array length.
Will Zelan
Ranch Hand

Joined: Apr 15, 2012
Posts: 56

Jeff Verdegan wrote:
Will Zelan wrote:
passing parameter to method i understand, so we are passing array,is it need to pass arraylength also?


Look at how the method uses that parameter. Work through what it would do if that parameter were less than the array length.

i just modified the program without using array.length in method,i got compile time error

in the above code i just mention int n=array.length,but why it cant accesible in for loop
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Will Zelan wrote:
in the above code i just mention int n=array.length,but why it cant accesible in for loop


The array variable is local to the main method. Your sort method's array is referenced by the a variable that's passed as an arg.
Will Zelan
Ranch Hand

Joined: Apr 15, 2012
Posts: 56


some little confusion parameter means the variable passed to method ,yes so we can passing as parameter to method.

what is argument,confused between argument and parameter?so we are trying to access array which is passed in method by using a variable is it
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

Will Zelan wrote:i just modified the program without using array.length in method,i got compile time error

What is the error? Compiler error messages contain a lot of useful information that explain what the exact problem is. Don't just say "I got an error", tell us exactly what the error is.

You've removed the parameter n from method bubble_srt. But you're still using n inside the method. The compiler ofcourse doesn't know anymore what n is inside the bubble_srt method.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Will Zelan
Ranch Hand

Joined: Apr 15, 2012
Posts: 56

Jesper de Jong wrote:
Will Zelan wrote:i just modified the program without using array.length in method,i got compile time error

What is the error? Compiler error messages contain a lot of useful information that explain what the exact problem is. Don't just say "I got an error", tell us exactly what the error is.

You've removed the parameter n from method bubble_srt. But you're still using n inside the method. The compiler ofcourse doesn't know anymore what n is inside the bubble_srt method.

oh ok, just see the above post which i post the program modified in the code

compile time error:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
It should be obvious from those error messages what the error is.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

Variables have a scope. They only exist within that scope. For local variables, the scope is from the point where the variable is declared until the } of the block in which the variable is declared.

You declare a local variable n in line 13. The scope of n is from line 13 to line 21. It doesn't exist outside that range of lines.

Local variables are never visible outside the method inside which they are declared. The local variable n which is declared in the main method is not visible in the bubble_srt method.
Will Zelan
Ranch Hand

Joined: Apr 15, 2012
Posts: 56

Jesper de Jong wrote:Variables have a scope. They only exist within that scope. For local variables, the scope is from the point where the variable is declared until the } of the block in which the variable is declared.

You declare a local variable n in line 13. The scope of n is from line 13 to line 21. It doesn't exist outside that range of lines.

Local variables are never visible outside the method inside which they are declared. The local variable n which is declared in the main method is not visible in the bubble_srt method.

oh ok, so thats why we are passing to method as parameter of variable array and length and again we are accessing those parameters in method as int a[] which is accesing array and int n which acessing length by providing identifier,is my explanation is correct
is there any way we can use "this" keyword in the above program
what is argument? is it different from parameter?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Always use one of the following idioms for for loops to iterate an array forward:-The latter is officially called the enhanced for loop, though many people call it a for‑each loop and you read the header, “for each Foo f in fooArray.” Note that the loop variable f must be regarded as read‑only. More details in the Java Tutorials and Language Specification, though the latter is often difficult to read.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Will Zelan wrote: . . . oh ok, so thats why we are passing to method as parameter of variable array and length and again we are accessing those parameters in method as int a[] which is accesing array and int n which acessing length by providing identifier,is my explanation is correct
No. The variable n only exists inside the other method. The parameter does not access anything. The reference to the array is sent as an argument.
is there any way we can use "this" keyword in the above program
No.

what is argument? is it different from parameter?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

It's not actually necessary to pass the length of the array to bubble_srt explicitly, because an array already knows its own length.


Will Zelan wrote:is there any way we can use "this" keyword in the above program

Why would you want to use the "this" keyword in the above program? I don't see why that would apply in any way.
Will Zelan
Ranch Hand

Joined: Apr 15, 2012
Posts: 56

Campbell Ritchie wrote:Always use one of the following idioms for for loops to iterate an array forward:-The latter is officially called the enhanced for loop, though many people call it a for‑each loop and you read the header, “for each Foo f in fooArray.” Note that the loop variable f must be regarded as read‑only. More details in the Java Tutorials and Language Specification, though the latter is often difficult to read.

i know it is enhanced for loop it is implemented in java5,is this related to above program,iam not geting what are you trying to say
i know parameter that is variables passing to method is called parameter,i want to know argument
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

There's a subtle difference between parameters and arguments, which is what Campbell tried to show.

A method can have parameters. You specify the parameters in the method declaration. When you call a method, then the actual value(s) that you are passing are the arguments.

Campbell Ritchie wrote:


Will Zelan
Ranch Hand

Joined: Apr 15, 2012
Posts: 56

Campbell Ritchie wrote:No. The variable n only exists inside the other method. The parameter does not access anything. The reference to the array is sent as an argument.
confused,reference to the array is sent as an argument so which is argument in the above program

here we are passing variable and arraylength as parameter to method is it fine

here what we are doing

f is variable passing to method so it is parameter

how f is argument here,previously it is an parameter
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jesper de Jong wrote:There's a subtle difference between parameters and arguments, which is what Campbell tried to show.

A method can have parameters. You specify the parameters in the method declaration. When you call a method, then the actual value(s) that you are passing are the arguments.


The two terms are often used interchangeably. Most of the time it's clear from the context what is meant, andit's not necessary to explicitly make the distinction. Personally, I can never remember which is which. I wouldn't get too hung up parameter vs. argument, but it's worth knowing that the distinction exists.

Sometimes you'll also see "formal parameters" for parameters and "actual parameters" (I think?) for arguments.
Will Zelan
Ranch Hand

Joined: Apr 15, 2012
Posts: 56

Jesper de Jong wrote:It's not actually necessary to pass the length of the array to bubble_srt explicitly, because an array already knows its own length.

oh k its working ,i think for understanding purpose we are passing parameter individually
Will Zelan wrote:is there any way we can use "this" keyword in the above program

Jesper de Jong wrote:Why would you want to use the "this" keyword in the above program? I don't see why that would apply in any way.

question wrong,this is used in constructor to acess the variables i think so
Will Zelan
Ranch Hand

Joined: Apr 15, 2012
Posts: 56

Campbell Ritchie wrote:No. The variable n only exists inside the other method. The parameter does not access anything. The reference to the array is sent as an argument.
confused,reference to the array is sent as an argument so which is argument in the above program

here we are passing variable and arraylength as parameter to method is it fine

here what we are doing

f is variable passing to method so it is parameter

how f is argument here,previously it is an parameter
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Do you understand the difference between declaring a method and calling a method?
Will Zelan
Ranch Hand

Joined: Apr 15, 2012
Posts: 56

Jeff Verdegan wrote:Do you understand the difference between declaring a method and calling a method?

yes, declaring method having variables as a parameters,when the method is calling the values passed to method is called arguments am i correct?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Will Zelan wrote:
Jeff Verdegan wrote:Do you understand the difference between declaring a method and calling a method?

yes, declaring method having variables as a parameters,when the method is calling the values passed to method is called arguments am i correct?


I meant, what does it mean to declare a method? What does a method declaration look like? What does it mean to call a method? What does a method declaration look like?

Can you tell me what method declarations and/or calls you see in this code?
Will Zelan
Ranch Hand

Joined: Apr 15, 2012
Posts: 56

Jeff Verdegan wrote:Do you understand the difference between declaring a method and calling a method?

Will Zelan wrote:yes, declaring method having variables as a parameters,when the method is calling the values passed to method is called arguments am i correct?


Jeff Verdegan wrote:I meant, what does it mean to declare a method? What does a method declaration look like? What does it mean to call a method? What does a method declaration look like?

Can you tell me what method declarations and/or calls you see in this code?

declare a method-- means which do some functions by providing variables as parameter

methods look like

To call a method means after creation of object for class to execute the function in the method.
The above program main() it is method and calling the args parameter in method.
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
is my answer on above post is correct
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

Declaring a method means: putting a new method in a class or interface. The first line of the method is called the method declaration; it tells you what the name of the method is, what its parameters are, what it returns etc. (see Defining Methods in Oracle's Java Tutorial).

Calling a method means: using the method, in other places in your program. When you call a method, you have to supply specific values for the parameters - these values are called the arguments.

The words "parameters" and "arguments" are sometimes used interchangeably. Don't get too much hung up on the exact words.

These are all very basic concepts. Make sure you understand the basics before trying to write more complicated programs, otherwise you'll be overwhelmed.

There are several good books for learning Java, such as Head First Java, and the Oracle Java Tutorials are a good online learning source. Also see the Bunkhouse for book reviews.
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Jesper de Jong wrote:Declaring a method means: putting a new method in a class or interface. The first line of the method is called the method declaration; it tells you what the name of the method is, what its parameters are, what it returns etc. (see Defining Methods in Oracle's Java Tutorial).

Calling a method means: using the method, in other places in your program. When you call a method, you have to supply specific values for the parameters - these values are called the arguments.

The words "parameters" and "arguments" are sometimes used interchangeably. Don't get too much hung up on the exact words.

These are all very basic concepts. Make sure you understand the basics before trying to write more complicated programs, otherwise you'll be overwhelmed.

There are several good books for learning Java, such as Head First Java, and the Oracle Java Tutorials are a good online learning source. Also see the Bunkhouse for book reviews.

iam struck up to explaining ,i can understand,even my previous post shows the code "how method look like" yes now i understand argument and parameters,even i told about in previous post,now iam cleared with arguments and parameters,now the only thing i have to know is which means passing parameters in a method it is clear,is it correct put semicolon for method
it is also method,but here int a[] and int n are arguments or accesing the parameters of previous method?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sekhar kiran wrote:now the only thing i have to know is which means passing parameters in a method it is clear


Yes, here we are calling the method, and passing array and array.length as parameters (or arguments--ignore the difference for now).

,is it correct put semicolon for method


It's correct to put a semicolon at the end of a statement. Often a statement is just a method call, or a method call with an assignment of the result to a variable. But sometimes a method call is embedded as an argument to another method, and sometimes method calls are chained. So we don't always immediately follow a method call with a semicolon.



it is also method,but here int a[] and int n are arguments or accesing the parameters of previous method?


It's impossible to say what this is without more context. It is either part of a method declaration, in which case int a[] and int n are telling us what arguments we'll have to pass when we call that method; or it's a syntax error. I don't know what you mean by "accessing the parameters of previous method," but whatever you mean, it's not that, as it has nothing to do with any "previous method".
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Jeff Verdegan wrote:It's correct to put a semicolon at the end of a statement. Often a statement is just a method call, or a method call with an assignment of the result to a variable. But sometimes a method call is embedded as an argument to another method, and sometimes method calls are chained. So we don't always immediately follow a method call with a semicolon.

ha yeah system.out.println(a); here println is a method, but suppose we declar void a(){} here we do not provide any semicolons why?

see the above program tell whats happening 14 and 23,im clear about 14th line
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sekhar kiran wrote:suppose we declar void a(){} here we do not provide any semicolons why?


That's just declaring a method that 1) Is package-private (hence no public, private or protected keyword), 2) Doesn't return anything (void), 3) doesn't take any arguments, and 4) doesn't do anything in it's body. It's like doing this:

except that it's not public.

We don't provide any semicolons because there are no statements. If we put something in the body, like

then we have a statement, which has a semicolon.



see the above program tell whats happening 15 and 24,im clear about 15th line


I assume you mean 14 and 23.

I don't know what to say. You've been told exactly what those are several times, and you even claimed you understand it.

If you don't know what's going on there, then you also don't know what's going on here:


or here:
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Jeff Verdegan wrote: public void a() {
// do nothing
}

yeah ofcourse i know void cannot return value and if we provide int instead of void it can return value,you are mentioning statemets what it means is it parameters
Jeff Verdegan wrote: Is package-private (hence no public, private or protected keyword ,except that it's not public.

i didnt get you

Jeff Verdegan wrote:I assume you mean 14 and 23.

I don't know what to say. You've been told exactly what those are several times, and you even claimed you understand it.

yes i told i understand ,iam very clear about 14th line,while in 23 rd line also iam clear it is a method ,but what the function is happing in the braces,there we int n that means its a array length,am i correct and int a[] means array values i think so

Jeff Verdegan wrote:If you don't know what's going on there, then you also don't know what's going on here:
public static void main(String a[])
System.out.println("Values Before the sort:\n");

i mean main() it is main method which is used to start the program and println() is also a method which is used to display statements,so here i got statements means with the brackets if we mention anything means we have to use semicolon like a(int a); and if we provide method with return type void so we does not want to mention semicolon like void a(int a)


Jeff Verdegan wrote: It is either part of a method declaration, in which case int a[] and int n are telling us what arguments we'll have to pass when we call that method

the above line what i understand means ie if we declare bubblesort(array,array.length); here array means variable which is defined in early and array.lenght means its an array value, so here we just passing parameter in bubblesort method and it has no return type so we use semicolon
while void bubblesort(int a[],int n){} here array=int a[] and array.length=int n ,if it is wrong or right
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

I'm sorry, but I have no idea what you're saying and I have no idea what you're still asking about. Maybe somebody else will be able to help you. Good luck!
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Jeff Verdegan wrote:I'm sorry, but I have no idea what you're saying and I have no idea what you're still asking about. Maybe somebody else will be able to help you. Good luck!
its ok thanks for your reply
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
1)let me clear my doubts,iam very clear about this bubble_srt(array, array.length); it is method declaration with passing arguments if it is correct say yes
2)here it is method call which passing arguments so then only array values and arraylength will be initialized here void bubble_srt( int a[], int n ),if it is correct say yes
3)why some methods have no semicolon(note iam clear about public static void main() and system.out.println():these two methods iam clear so i dont want to interfere here)for example see 1) 2) here both 1 and 2 are methods but 1 have no semicolon and 2 have semicolon,so when should we use it semicolon
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sekhar kiran wrote:1)let me clear my doubts,iam very clear about this bubble_srt(array, array.length); it is method declaration with passing arguments if it is correct say yes


No. That is a method call, not a declaration. A declaration includes a return type, types for the parameters, and usually an access specifier.

2)here it is method call which passing arguments so then only array values and arraylength will be initialized here void bubble_srt( int a[], int n ),if it is correct say yes


No. That is a method declaration.

Also note that in a declaration, we're not passing any arguments. We're just stating what arguments must be passed when calling this method




3)why some methods have no semicolon(note iam clear about public static void main() and system.out.println():these two methods iam clear so i dont want to interfere here)for example see 1)


Every statement ends with a semicolon.

A method call ends with a semicolon unless it's in a chain and not the final method, or passed as an expression to another method or constructor, or is an operad to an operator and not the final token in its statment. In other words, whenever a method call is the last thing in a statement, there is a semicolon after that method call.

There is a semicolon after an abstract method declaration.

There is no semicolon after a concrete method declaration. Your #1 appears to be a concrete method declaration, so no semicolon.

main() and println() are exactly like every other method, so I don't know what you mean when you say you are clear about those two methods. There's nothing special or different about them.

In your #1, there's no semicolon after void a() because that is a method declaration, not a statement. We do put a semicolon after a method declaration unless it's an abstract method.

Your #2 it not legal java syntax. It does not exist in Java. If you mean for that to be a method call, then its a(a, b), without the type declarations for the parameters. If you mean for it to be a method declaration, then it has to include a return type, and it either has to include the abstract keyword and be followed by a semicolon, or it has to have a method body enclosed in { }.



You're making this much more complicated than it actually is.
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
1)so as your explanataion bubble(array,array.length) it is method call which passing argument for the method declaration void bubble(int a[],int n)
2)passing arguments to a method should be end with semicolon,is it both are correct
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sekhar kiran wrote:1)so as your explanataion bubble(array,array.length) it is method call which passing argument for the method declaration void bubble(int a[],int n)


Yes.

2)passing arguments to a method should be end with semicolon,is it both are correct


I already explained the rules for where to put a semicolon. Nowhere in those rules is there anything about passing arguments to a method. The rules do mention calling vs. declaring, however.

In all the time you've spent worrying about the details of where to put and not put semicolons, you could have written a lot of code and been learning firsthand. If you forget one, the compiler will let you know.
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Jeff Verdegan wrote:I already explained the rules for where to put a semicolon. Nowhere in those rules is there anything about passing arguments to a method. The rules do mention calling vs. declaring, however.

In all the time you've spent worrying about the details of where to put and not put semicolons, you could have written a lot of code and been learning firsthand. If you forget one, the compiler will let you know.

yeah you told that a method call ends with a semicolon unless some restriction are there i got it,but the basic thing whenever calling method should be semicolon,am i right
Jeff Verdegan wrote: it's in a chain and not the final method, or passed as an expression to another method or constructor, or is an operad to an operator and not the final token in its statment
can you give me some simple example which mentioned above quote ,its very helpful to me

compile time error whats the problem
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sekhar kiran wrote:the basic thing whenever calling method should be semicolon,am i right


I already answered that question twice. I'm not answering it again.

Jeff Verdegan wrote: it's in a chain and not the final method, or passed as an expression to another method or constructor, or is an operad to an operator and not the final token in its statment
can you give me some simple example which mentioned above quote ,its very helpful to me


I already did for some of them. I'm not going to repeat myself. For the others, I'm going to leave that as an exercise for you.

If you want to learn, you need to be a more active participant in your own education, rather than just sitting back helplessly, waiting for people to shove information down your throat.

compile time error whats the problem


The error message is telling you what the problem is. If you don't understand the error and you want somebody here to help you with it, then copy/paste the exact, complete error message here, and indicate clearly which line is causing it.
 
 
subject: doubt in some programs and question in java