Local variable is redundant java

«Local variable is redundant» using Java

String class that create new String objects by combining several strings, arrays of strings, or objects. Creating Strings Using a Class Constructor You can use overloads of the String class constructor to create strings from character arrays.

«Local variable is redundant» using Java

Why is the following giving me a «Local variable is redundant error»?

public double depreciationAmount()

Why is the following giving me a «local variable is redundant error»?

Because you can trivially write this without using a local variable.

public double depreciationAmount()

Hence the local variable is deemed to be unnecessary / redundant by the checker.

However, I surmise that this is not a compiler error . It might be a compiler warning , or more likely it is a style checker or bug checker warning. It is something you could ignore without any risk to the correctness of your code . as written.

Also, I would predict that once that the code has been JIT compiled (by a modern Hotspot JIT compiler . ) there would be no performance difference between the two versions.

I won’t attempt to address the issue as to whether the warning is appropriate 1 . If you feel it is inappropriate, then «Local variable is redundant» using Java explains how to suppress it.

1 — Except to say that it is too much to expect current generation style checkers to know when so-called explaining variables are needed. First you’d need to get a statistically significant 2 group of developers to agree on measurable 3 criteria for when the variables are needed, and when they aren’t.
2 — Yea, I know. Abuse of terminology.
3 — They must be measurable, and there needs to be consensus on what the thresholds should be if this is to be implemented by a checker.

Although not the case here, if having a redundant local variable is desired (I’ve had one time where this was the case — without getting into specifics), here’s how to suppress this specific warning.

@SuppressWarnings("UnnecessaryLocalVariable") public double depreciationAmount()

You only use the value of percentDepreciated to return it when you could have just done return (cost * percentDepreciated) .

Why is the following giving me a «local variable is redundant error»?

I believe this message is wrong. Your depreciationAmount variable assignment is totally fine. Moreover, I always prefer this kind of assignment before return, because it helps to avoid confusion while debugging.

In this example, the getValue() method returns an expression result, instead of assigning the expression result to a variable.

Enter image description here

Now when I use debugger watch, to know the result of an expression, I got a confusion. My program ends with the wrong result and debugger watch values are inconsistent. It would be easy to avoid this, if I would have a variable assigned before the returning expression:

Integer value = 1 + getCounter(); return value; 

Now I can put a breakpoint at the return statement and know what was the result of the expression, before it was returned. Also I do not need the expression in the watch any more, and code will be executed correctly while debugging.

Why does .ToString() on a null string cause a null error, This is all correct, but also note that selectedItem.Cost.GetType() will throw if Cost is null of type int?.The difference is that ToString is a virtual method which is overridden by Nullable and so can be called on the int? directly. On the other hand GetType is a non-virtual method declared on a base class of …

Creating New Strings in .NET

.NET allows strings to be created using simple assignment, and also overloads a class constructor to support string creation using a number of different parameters. .NET also provides several methods in the System.String class that create new String objects by combining several strings, arrays of strings, or objects.

Creating Strings Using Assignment

The easiest way to create a new string object is simply to assign a string literal to a String object.

Creating Strings Using a Class Constructor

You can use overloads of the String class constructor to create strings from character arrays. You can also create a new string by duplicating a particular character a specified number of times.

Methods that Return Strings

The following table lists several useful methods that return new string objects.

Method name Use
String.Format Builds a formatted string from a set of input objects.
String.Concat Builds strings from two or more strings.
String.Join Builds a new string by combining an array of strings.
String.Insert Builds a new string by inserting a string into the specified index of an existing string.
String.CopyTo Copies specified characters in a string into a specified position in an array of characters.
Format

You can use the String.Format method to create formatted strings and concatenate strings representing multiple objects. This method automatically converts any passed object into a string. For example, if your application must display an Int32 value and a DateTime value to the user, you can easily construct a string to represent these values using the Format method. For information about formatting conventions used with this method, see the section on composite formatting.

The following example uses the Format method to create a string that uses an integer variable.

int numberOfFleas = 12; string miscInfo = String.Format("Your dog has fleas. " + "It is time to get a flea collar. " + "The current universal date is: .", numberOfFleas, DateTime.Now); Console.WriteLine(miscInfo); // The example displays the following output: // Your dog has 12 fleas. It is time to get a flea collar. // The current universal date is: 2008-03-28 13:31:40Z. 
Dim numberOfFleas As Integer = 12 Dim miscInfo As String = String.Format("Your dog has fleas. " & _ "It is time to get a flea collar. " & _ "The current universal date is: .", _ numberOfFleas, Date.Now) Console.WriteLine(miscInfo) ' The example displays the following output: ' Your dog has 12 fleas. It is time to get a flea collar. ' The current universal date is: 2008-03-28 13:31:40Z. 

In this example,DateTime.Now displays the current date and time in a manner specified by the culture associated with the current thread.

Concat

The String.Concat method can be used to easily create a new string object from two or more existing objects. It provides a language-independent way to concatenate strings. This method accepts any class that derives from System.Object . The following example creates a string from two existing string objects and a separating character.

string helloString1 = "Hello"; string helloString2 = "World!"; Console.WriteLine(String.Concat(helloString1, ' ', helloString2)); // The example displays the following output: // Hello World! 
Dim helloString1 As String = "Hello" Dim helloString2 As String = "World!" Console.WriteLine(String.Concat(helloString1, " "c, helloString2)) ' The example displays the following output: ' Hello World! 
Join

The String.Join method creates a new string from an array of strings and a separator string. This method is useful if you want to concatenate multiple strings together, making a list perhaps separated by a comma.

The following example uses a space to bind a string array.

string[] words = ; Console.WriteLine(String.Join(" ", words)); // The example displays the following output: // Hello and welcome to my world! 
Dim words() As String = Console.WriteLine(String.Join(" ", words)) ' The example displays the following output: ' Hello and welcome to my world! 
Insert

The String.Insert method creates a new String by inserting a string into a specified position in another string. This method uses a zero-based index. The following example inserts a string into the fifth index position of MyString and creates a new string with this value.

string sentence = "Once a time."; Console.WriteLine(sentence.Insert(4, " upon")); // The example displays the following output: // Once upon a time. 
Dim sentence As String = "Once a time." Console.WriteLine(sentence.Insert(4, " upon")) ' The example displays the following output: ' Once upon a time. 
CopyTo

The String.CopyTo method copies portions of a string into an array of characters. You can specify both the beginning index of the string and the number of characters to be copied. This method takes the source index, an array of characters, the destination index, and the number of characters to copy. All indexes are zero-based.

The following example uses the CopyTo method to copy the characters of the word «Hello» from a string object to the first index position of an array of characters.

string greeting = "Hello World!"; char[] charArray = ; Console.WriteLine("The original character array: ", new string(charArray)); greeting.CopyTo(0, charArray,0 ,5); Console.WriteLine("The new character array: ", new string(charArray)); // The example displays the following output: // The original character array: Where // The new character array: Hello 
Dim greeting As String = "Hello World!" Dim charArray() As Char = Console.WriteLine("The original character array: ", New String(charArray)) greeting.CopyTo(0, charArray, 0, 5) Console.WriteLine("The new character array: ", New String(charArray)) ' The example displays the following output: ' The original character array: Where ' The new character array: Hello 

Java — Variable initializer redundant, The warning simply means that the empty string with which the line variable is initialized is useless. Before line is read, it is being assigned in the loop declaration: line = bufferedReader.readLine () So assigning line = «» is redundant. You can leave it uninitialized: Code sampleString line;while((line = bufferedReader.readLine()) != null) Feedback

How to disable warning: Using ‘stringWithString’ with a literal is redundant?

Some old third party code gives warning:

Using ‘stringWithString’ with a literal is redundant

Instead of modifying the source codes, I’d prefer to disable the warning in Xcode. What is the compiler switch to disable this specific warning?

The warning message should tell you the name of the warning. You can then turn that warning off.

test.m:4:5: warning: using 'stringWithString:' with a literal is redundant [-Wobjc-redundant-literal-use] 

so you want to add the flag -Wno-objc-redundant-literal-use to the compiler flags for that file or that project.

  1. Select your target .
  2. Go to the compiled sources.
  3. Select your .m file and set flag -w to suppress all the warning of that file .

Creating New Strings in .NET, The String.Insert method creates a new string by inserting a string into a specified position in another string. This method uses a zero-based index. The following example inserts a string into the fifth index position of MyString and creates a new string with this value. string sentence = «Once a time.»;

Why is this cast redundant?

I have a method with the following overloads:

string Call(string function, Dictionary parameters, object body) string Call(string function, Dictionary parameters, JObject body) 

Now I added another overload:

string Call(string function)

I added a cast to JObject so the compiler knows which overload it should use. But Visual Studio tells me that the cast is redundant. But why isn’t my call ambiguous without the cast?

But why isn’t my call ambiguous without the cast?

Because the overload with the JObject parameter is «better» than the overload with the object parameter. because the conversion from null to JObject is «better» than the conversion from null to object .

JObject is more specific than object , because there’s an implicit conversion from JObject to object , but not vice versa.

If the final parameter for the first method were string instead (for example) then neither overload would be better than the other, and the call would be ambiguous without the cast.

See section 7.5.3 of the C# 5 specification for all the intricate details. In particular, section 7.5.3.5 («better conversion target») is relevant here.

Java — Why does findbugs give redundant nullcheck for, You have to understand what redundant Null-Check means. When Findbugs gives you this warning, it means you did a Null-Check two times, and the second time is not necessary. Which matches your example. In the first code, new String («foo») does an implicit null check, because new String (null) will throw …

Источник

Читайте также:  Nginx apache php mysql linux
Оцените статью