Documente Academic
Documente Profesional
Documente Cultură
class Program
char[] ch = str.ToCharArray();
if (i % 2 == 0)
ch[i] = char.ToUpper(ch[i]);
else
ch[i] = char.ToLower(ch[i]);
Techno Planners Dot Net
}
strbld.Append(ch[i].ToString());
Console.WriteLine(strbld.ToString());
QWERTY-HGFDS-L9789-8967Y-NBHGF
QASDFG-CD90A-BL65X-OPBND-IUYTR
CVBNMH-BVT68-XCZ5R-ZXIU7-HYTRE
SDFG58-C85S4-DCZ5R-HYTRE-NBHGTR
Techno Planners Dot Net
8K subscribers
JDBC : https://goo.gl/AhvbC8
XML : https://goo.gl/aKx4aA
Maven : https://goo.gl/pyvK53
Hibernate : https://goo.gl/wdpZaJ
Spring : https://goo.gl/RPDDe8
6. java 8 in action
Techno Planners Dot Net
Java return Examples
Use the return keyword in methods. Return multiple values, return expressions and fix errors.
Return. A stone is thrown into the air. It comes back down to the ocean and makes a splash. It returns.
In Java too methods return.
In a return statement, we evaluate expressions—and as part of this evaluation, other methods may run.
Types must match, or an error occurs.
Expression. We can return a single value. But often we want a more complex return statement—we use
an expression after the return keyword.
ComputeSize: This method receives two arguments, both of type int. In the return expression, the two
numbers are multiplied.
And: The evaluated result is returned to the calling location. The expression itself is not returned, just its
result.
}
Techno Planners Dot Net
public static void main(String[] args) {
System.out.println(result);
Output
30
Return method result. In a return statement, we can invoke another method. In this example, we return
the result of cube() when getVolume() returns.
And: The cube method itself returns the result of Math.pow, a built-in mathematics method.
return cube(size);
System.out.println(volume);
Output
Return, void method. In a void method, an implicit (hidden) return is always at the end of the method.
But we can specify a return statement (with no argument) to have an early exit.
Here: If the "password" argument has a length greater than or equal to 5, we return early. Otherwise we
print a warning message.
if (password.length() >= 5) {
return;
displayPassword("furball");
displayPassword("cat");
Output
Password: furball
Password: cat
Boolean. We can use an expression to compose a boolean return value. This is a powerful technique—
we combine several branches of logic into a single statement.
Boolean
And: The result of isValid is a boolean. Both logical conditions must be satisfied for isValid to return true.
System.out.println(isValid("green", true));
System.out.println(isValid("", true));
System.out.println(isValid("orchard", false));
Output
true
false
false
Compilation error. A method that is supposed to return a value (like an int) must return that value.
Otherwise a helpful compilation error occurs.
if (id.length() <= 4) {
System.out.println("Short");
System.out.println(result);
Output
at Program.getResult(Program.java:3)
at Program.main(Program.java:13)
Techno Planners Dot Net
Multiple return values. This is a common programming problem. To return two values, we can use a
class argument—then we set values within that class.
Class
Result: This has the same effect as returning two arguments. The syntax is more verbose. The object can
be reused between method calls.
Tip: It is often a better design decision to have related methods on a class. Then those methods simply
modify fields of the class.
class Data {
data.name = "Java";
data.size = 100;
getTwoValues(data);
System.out.println(data.name);
System.out.println(data.size);
Output
Java
100
Unreachable code. This is a fatal error in Java. If code cannot be reached, we get a java.lang.Error.
To fix this program, we must remove the "return" or the final, unreachable statement.
System.out.println("Hello");
return;
System.out.println("World");
}
Techno Planners Dot Net
Output
Unreachable code
at Program.main(Program.java:9)
Type mismatch error. We cannot assign a variable to void method. This returns in a "type mismatch"
error. Void means "no value."
System.out.println(123);
}
Techno Planners Dot Net
Output
at Program.main(Program.java:11)
Performance. In my tests, I found that the JVM does a good job of inlining methods. So if we return a
statement that evaluates into another method call,
Also: I found that expressions with temporary local variables were optimized well, to match single-
expression return values.
A review. The return keyword signals an end to a method. It accepts an argument of a type that matches
the method's signature.
package Sample1;
int d=40;
int a=5;
int b=10;
return res;
return cube(size);
int sum=add();
System.out.println("result= "+sum);
System.out.println("cube= "+volume);
System.out.println("computSize= "+x);
System.out.println("computSize1= "+result);