variable-assignment

Java - Order of Operations - Using Two Assignment Operators in a Single Line

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 14:45:02
问题 What are the order of operations when using two assignment operators in a single line? public static void main(String[] args){ int i = 0; int[] a = {3, 6}; a[i] = i = 9; // this line in particular System.out.println(i + " " + a[0] + " " + a[1]); } Edit: Thanks for the posts. I get that = takes values from the right, but when I compile this I get: 9 9 6 I thought it would have been and ArrayOutOfBounds exception, but it is assigning 'a[i]' before it's moving over the 9. Does it just do that

Can you declare multiple variables at once in Go?

丶灬走出姿态 提交于 2019-11-26 14:18:30
问题 Is it possible to declare multiple variables at once using Golang? For example in Python you can type this: a = b = c = 80 and all values will be 80. 回答1: Yes, you can: var a, b, c string a = "foo" fmt.Println(a) You can do something sort of similar for inline assignment, but not quite as convenient: a, b, c := 80, 80, 80 回答2: In terms of language specification, this is because the variables are defined with: VarDecl = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) . VarSpec = IdentifierList (

What is this kind of assignment in Python called? a = b = True

99封情书 提交于 2019-11-26 14:08:03
问题 I know about tuple unpacking but what is this assignment called where you have multiple equals signs on a single line? a la a = b = True It always trips me up a bit especially when the RHS is mutable, but I'm having real trouble finding the right keywords to search for in the docs. 回答1: It's a chain of assignments and the term used to describe it is... - Could I get a drumroll please? Chained Assignment . I just gave it a quite google run and found that there isn't that much to read on the

memcpy vs assignment in C

谁说胖子不能爱 提交于 2019-11-26 14:06:35
问题 Under what circumstances should I expect memcpys to outperform assignments on modern INTEL/AMD hardware? I am using GCC 4.2.x on a 32 bit Intel platform (but am interested in 64 bit as well). 回答1: You should never expect them outperform assignments. The reason is, the compiler will use memcpy anyway when it thinks it would be faster (if you use optimize flags). If not and if the structure is reasonable small that it fits into registers, direct register manipulation could be used which wouldn

Why does a space in a variable assignment give an error in Bash? [duplicate]

荒凉一梦 提交于 2019-11-26 12:30:50
This question already has an answer here: Assignment of variables with space after the (=) sign? 3 answers #!/bin/bash declare -r NUM1=5 NUM2 =4 # Line 4 num3=$((NUM1 + NUM2)) num4=$((NUM1 - NUM2)) num5=$((NUM1 * NUM2)) num6=$((NUM1 / NUM2)) # Line 9 echo "$num3" echo $((5**2)) echo $((5%4)) I am using this bash script, and when I was running the script, I got the error ./bash_help ./bash_help: line 4: NUM2: command not found ./bash_help: line 9: NUM1 / NUM2: division by 0 (error token is "NUM2") 5 25 1 So I have changed the code to this and the error was gone. #!/bin/bash declare -r NUM1=5

How to assign a variable in an IF condition, and then return it?

别说谁变了你拦得住时间么 提交于 2019-11-26 12:29:59
问题 def isBig(x): if x > 4: return \'apple\' else: return \'orange\' This works: if isBig(y): return isBig(y) This does NOT work: if fruit = isBig(y): return fruit Why doesn\'t the 2nd one work!? I want a 1-liner. Except, the 1st one will call the function TWICE. How to make it 1 liner, without calling the function twice? 回答1: I see somebody else has already pointed to my old "assign and set" Cookbook recipe, which boils down in its simplest version to: class Holder(object): def set(self, value):

Indirect variable assignment in bash

泪湿孤枕 提交于 2019-11-26 12:23:33
Seems that the recommended way of doing indirect variable setting in bash is to use eval : var=x; val=foo eval $var=$val echo $x # --> foo The problem is the usual one with eval : var=x; val=1$'\n'pwd eval $var=$val # bad output here (and since it is recommended in many places, I wonder just how many scripts are vulnerable because of this...) In any case, the obvious solution of using (escaped) quotes doesn't really work: var=x; val=1\"$'\n'pwd\" eval $var=\"$val\" # fail with the above The thing is that bash has indirect variable reference baked in (with ${!foo} ), but I don't see any such

Why can not I add two bytes and get an int and I can add two final bytes get a byte?

白昼怎懂夜的黑 提交于 2019-11-26 11:50:24
public class Java{ public static void main(String[] args){ final byte x = 1; final byte y = 2; byte z = x + y;//ok System.out.println(z); byte a = 1; byte b = 2; byte c = a + b; //Compiler error System.out.println(c); } } If the result of an expression involving anything int-sized or smaller is always an int even if the sum of two bytes fit in a byte. Why does it happen when we add two final bytes that fit in a byte? There is no compiler error. From the JLS 5.2 Assignment Conversion In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int : - A

Operator precedence with Javascript Ternary operator

感情迁移 提交于 2019-11-26 11:42:42
I cant seem to wrap my head around the first part of this code ( += ) in combination with the ternary operator. h.className += h.className ? ' error' : 'error' The way i think this code works is as following: h.className = h.className + h.className ? ' error' : 'error' But that isn't correct because that gives a error in my console. So my question is how should i interpet this code correctly? Kobi h.className = h.className + (h.className ? ' error' : 'error') You want the operator to work for h.className , better be specific about it. Of course, no harm should come from h.className += ' error'

is it possible to use variables in remote ssh command?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 11:34:47
问题 I\'d like to execute several commands in sequence on a remote machine, and some of the later commands depend on earlier ones. In the simplest possible example I get this: ssh my_server \"echo this is my_server; abc=2;\" this is my_server abc=2: Command not found. ssh my_server \"echo this is my_server; abc=2; echo abc is $abc\" abc: undefined variable For a bit of background info, what I actually want to do is piece together a path and launch a java application: ssh my_server \'nohup sh -c \"