local-variables

Why each slot of the local variable array in a stack frame is of 4 bytes, and not 1 byte in the JVM?

坚强是说给别人听的谎言 提交于 2020-01-04 11:03:29
问题 Each slot of local variable array is of 4-bytes. So to store a character, short or byte variables one slot is used. Means all smaller data-types internally gets converted into int data type. My doubt is: 1). Is it not making smaller data types useless, if internally they are of 4-bytes?, If yes, Why not remove such data-types from the language? 2). If each slot is of 1-byte then there will be no wastage of memory. Why not each slot is of 1-byte? 回答1: 1). Is it not making smaller data types

Table variable and exec

为君一笑 提交于 2020-01-04 01:57:10
问题 How can I use a table variable while executing a command string? DECLARE @FileIDs TABLE ( File_ID int ) insert into @FileIDs select ID from Files where Name like '%bla%'; DECLARE @testquery as varchar(max); set @testquery = 'select * from @FileIDs'; exec(@testquery); returns the following error Msg 1087, Level 15, State 2, Line 1 Must declare the table variable "@FileIDs". 回答1: The table @FileIDs is not in the scope of exec(@testquery) . That's why you have that problem. To solve this problem

“Java concurrency in practice” - cached thread-safe number factorizer (Listing 2.8)

Deadly 提交于 2020-01-03 09:41:13
问题 In the following code (copied from Java Concurrency in Practice Chapter 2, section 2.5, Listing 2.8): @ThreadSafe public class CachedFactorizer implements Servlet { @GuardedBy("this") private BigInteger lastNumber; @GuardedBy("this") private BigInteger[] lastFactors; @GuardedBy("this") private long hits; @GuardedBy("this") private long cacheHits; public synchronized long getHits() { return hits; } public synchronized double getCacheHitRatio() { return (double) cacheHits / (double) hits; }

“Java concurrency in practice” - cached thread-safe number factorizer (Listing 2.8)

别说谁变了你拦得住时间么 提交于 2020-01-03 09:41:07
问题 In the following code (copied from Java Concurrency in Practice Chapter 2, section 2.5, Listing 2.8): @ThreadSafe public class CachedFactorizer implements Servlet { @GuardedBy("this") private BigInteger lastNumber; @GuardedBy("this") private BigInteger[] lastFactors; @GuardedBy("this") private long hits; @GuardedBy("this") private long cacheHits; public synchronized long getHits() { return hits; } public synchronized double getCacheHitRatio() { return (double) cacheHits / (double) hits; }

Use var in field declaration

五迷三道 提交于 2020-01-02 10:00:32
问题 So starting in Java 9, we can use var to declare local variables: var s = "cool"; is there a way to use a similar construct when declaring fields? class Container { final var s = "cool"; // does not compile tmk } doesn't seem like it from what I can tell. 回答1: is there a way to use a similar construct when declaring fields? No. According to JEP 286: Local-Variable Type Inference: This treatment would be restricted to local variables with initializers, indexes in the enhanced for-loop, and

How to dynamically define a class method which will refer to a local variable outside?

一个人想着一个人 提交于 2019-12-31 12:59:34
问题 class C end var = "I am a local var outside" C.class_eval do def self.a_class_method puts var end end I know, this is not correct, because the def created a new scope. I also know that use define_method can create a instance method without creating a new scope, but my point is how to define a class method . 回答1: Class methods don't really exist in Ruby, they are just singleton methods of the class object. Singleton methods don't really exist, either, they are just ordinary instance methods of

How can non-final fields be used in a anonymous class class if their value can change?

元气小坏坏 提交于 2019-12-31 04:04:09
问题 I asked this question before but I didn't get an appropriate answer. How can non-final fields be used in a anonymous class class if their value can change? class Foo{ private int i; void bar(){ i = 10 Runnable runnable = new Runnable (){ public void run (){ System.out.println(i); //works fine }//end method run }//end Runnable }//end method bar }//end class Foo If the local variables which are used inside an anonymous class must be final to enable the compiler inlining their values inside the

`defined?` and `unless` not working as expected

纵饮孤独 提交于 2019-12-30 17:26:20
问题 I was expecting the following snippet: var = "Not Empty" unless defined? var var # => nil to return "Not Empty" , but I got nil . Any insight into why this is happening? 回答1: This is one of the only moments in Ruby I would call actual WTFs. You have to use unless defined? var var = :value end With the postfix syntax, the interpreter will internally nil -ify the value so it can reason about the variable, thus making it defined before the check is done: # Doesn't print anything unless defined?

`defined?` and `unless` not working as expected

╄→尐↘猪︶ㄣ 提交于 2019-12-30 17:26:10
问题 I was expecting the following snippet: var = "Not Empty" unless defined? var var # => nil to return "Not Empty" , but I got nil . Any insight into why this is happening? 回答1: This is one of the only moments in Ruby I would call actual WTFs. You have to use unless defined? var var = :value end With the postfix syntax, the interpreter will internally nil -ify the value so it can reason about the variable, thus making it defined before the check is done: # Doesn't print anything unless defined?

why is global variable not accessible even if local variable is defined later in code [duplicate]

£可爱£侵袭症+ 提交于 2019-12-25 06:54:11
问题 This question already has answers here : Javascript function scoping and hoisting (16 answers) Closed 5 years ago . why does the following code segment generate the following output? code segment: var a = 10; function(){ console.log(a); var a = 5; } output: undefined 回答1: Because variable is hoisted at top and in your function you have declared the variable var a = 5 which is same as following: var a = 10; function(){ var a; // a = undefined console.log(a);//a is not defined so outputs