Doubt about parenthesis () in JAVA

Doubt about parenthesis () in JAVA
0.0 0

#1

Hi campers!
I write this message because I feel that I really dont understand how works the parenthesis in JAVA (I know, sound silly).
I know that in maths parenthesis establishes the order of resolution

((5+1) + 2) = (6 + 2)

But what about in JAVA?

For exmple if in this exercise:

// Setup**
var myArray = [];**
// Only change code below this line.**
for (var i = 1; i < 6; i++) {
  myArray.push(i);**
}

What happen with the i between parenthesis ? , Is myArray.push multiplied for (i)?

Thanks !


#2

I think first you need to correct the lines about “… () in JAVA”, because, JAVA and JAVASCRIPT are both different languages.
And that question can be discuss in general perspective, like, Doubt about parenthesis () in programming languages.
So, usually, there are two cases where parenthesis are used, with methods / functions or with constructors, but what’s there usage ?
They used to define parameters at the time of defining any method / constructor and when we call them, we just passing the required arguments, as you did that with array:

myArray.push( argument )

#3

Do you mean javascript not java?

javascript and java are very different languages - explained here

freecodecamp offers javascript learning - your code looks like a javascript for-loop so I’ll assume you mean javascript

parentheses in javascript are generally used to group a block of code - the meaning of the grouping depends on the context - in your code myArray.push(i) the context is a function call since myArray is an array and push is an array function

so (i) groups the actual parameters aka arguments for the call to push - this means the value of i is passed to push as its first and only parameter - the effect is the value of i is pushed onto myArray

this is a great guide for anyone learning javascript


#4

i think, curly braces { ... } they are used to group a block of code not parenthesis, correct me if i am at wrong ?


#5

parentheses and braces both are used for grouping - both have a variety of meanings depending on the context - parentheses often have arithmetic use like the one OP asked about e.g.

x=5 + 2 * 3 // x=11
x=(5 + 2) * 3 // x=21

but the code grouped by parentheses can be non-arithmetic expressions

Rather than list all the ways parentheses and braces can group code in javascript it is better to keep in mind javascript like all computer programming languages was designed by one or few persons - these persons decide what characters to use for the syntax and grammar of the language and what those characters mean - it is somewhat but not completely arbitrary - there may be similarities to other languages like human language or mathematical language but some similarities can be quite deceptive

point being don’t go about trying to intuit or reason about language syntax - unless you’re a prorgamming language designer yourself - it is what it is - go to the language reference for the construct you intend to use or understand and read what every single character means


#6

Actually that user already told i know how it works in arithmetic expressions and in all languages it is same for the math, but differ in its own language syntax and that use is actually concerned with the use of parenthesis with methods or objects like ...(), in that case they only used to accept arguments.
In addition with your argument, () also can also be use with ternary operator.


#7

Parentheses indicate to the compiler that you want to evaluate expressions inside them. The compiler will execute from the innermost to the outermost. It is the same as you would see in maths. So

(4 + (2 * (6 - 3)))
// - is an infix function with arguments 6 and 3, apply that
(4 + (2 * 3))
// * is an infix function with arguments 2 and 3, apply that
(4 + 6)
// + is an infix function with arguments 4 and 6, apply that
10

When the parentheses come directly after an identifier (a variable name), the compiler will assume the identifier refers to a function, so again, it will evaluate what is in the parentheses to values, then attempt to apply the resulting values to a function.

A function in Javascript is a prefixed expression. + or - are infix. With a defined function in JS you put the identifier before your values/expressions, which go in the parentheses. Some languages let you define infix functions or postfix functions; JS does not.

You define what the [prefix] expression should do by using the function keyword, this denotes that it is a description of how the compiler should convert the expression to a value:

function add(a,b) { return a + b }

Then you use it:

add(4, (2 * (6 - 3)))
add(4, (2 * 3))
add(4, 6)
10

Curly brackets are to denote blocks in JavaScript, eg a collection of related bits of code that should be evaluated together. They are also used to denote objects, the mechanism is the same:

{
  // These things are scoped inside this block:
  const foo = 'bar';
  const baz = 1;
}

// This is an object, note the `:` and the commas at the end of the lines:
{
  foo: 'bar',
  baz: 1,
}

So in the example, you’re using a keyword for, which is the special syntax used to define a for loop in JS. It isn’t quite the same as a function - the for loop itself isn’t an expression, so doesn’t quite work the same way as a normal function/expression would (it doesn’t evaluate to a value, it just runs some code over and over). They work in very much the same way: the expression in the parentheses is evaluated, then the code inside the block ({, }) is run. The for tells the compiler to go back and evaluate the expression again then run the code in the block until a condition is met:

// for (value to initialise loop; end condition; expression to evaluate on each loop)

for (var i = 1; i < 6; i++) { myArray.push(i); }
// i is 1, so i is less than 6. Push 1 to myArray. Loop - i++ means i goes up by one

(i = 2; i < 6; i++) { myArray.push(i); }
// i is 2, so i is less than 6. Push 2 to myArray. Loop - i++ means i goes up by one

(i = 3; i < 6; i++) { myArray.push(i); }
// i is 3, so i is less than 6. Push 3 to myArray. Loop - i++ means i goes up by one

(i = 4; i < 6; i++) { myArray.push(i); }
// i is 4, so i is less than 6. Push 4 to myArray. Loop - i++ means i goes up by one

(i = 5; i < 6; i++) { myArray.push(i); }
// i is 5, so i is less than 5. Push 5 to myArray. Loop - i++ means i goes up by one

(i = 6; i < 6; i++) { myArray.push(i); }
// i is 6, so i is NOT less than 6. Stop looping

note
expression: a collection of values/variables/functions/etc that the computer can evaluate to a value.
identifier: a name that identifieds a unique object, in JS, a variable


#8

Thank you for your answer :slightly_smiling_face: , all are useful for me.

Now I understand better how to use parentheses :slightly_smiling_face: in JavaScript


#9

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.