public static int funcName(int a, int b) {
// body
}
modifier returnType nameOfMethod (Parameter List) {
// method body
}
/** the snippet returns the minimum between two numbers */
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
System.out.println("wiki.jikexueyuan.com!");
int result = sum(6, 9);
public class ExampleMinNumber{
public static void main(String[] args) {
int a = 11;
int b = 6;
int c = minFunction(a, b);
System.out.println("Minimum Value = " + c);
}
/** returns the minimum of two numbers */
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
}
Minimum value = 6
public class ExampleVoid {
public static void main(String[] args) {
methodRankPoints(255.7);
}
public static void methodRankPoints(double points) {
if (points >= 202.5) {
System.out.println("Rank:A1");
}
else if (points >= 122.4) {
System.out.println("Rank:A2");
}
else {
System.out.println("Rank:A3");
}
}
}
Rank:A1
public class swappingExample {
public static void main(String[] args) {
int a = 30;
int b = 45;
System.out.println("Before swapping, a = " +
a + " and b = " + b);
// Invoke the swap method
swapFunction(a, b);
System.out.println("n**Now, Before and After swapping values will be same here**:");
System.out.println("After swapping, a = " +
a + " and b is " + b);
}
public static void swapFunction(int a, int b) {
System.out.println("Before swapping(Inside), a = " + a
+ " b = " + b);
// Swap n1 with n2
int c = a;
a = b;
b = c;
System.out.println("After swapping(Inside), a = " + a
+ " b = " + b);
}
}
Before swapping, a = 30 and b = 45 Before swapping(Inside), a = 30 b = 45 After swapping(Inside), a = 45 b = 30 **Now, Before and After swapping values will be same here**: After swapping, a = 30 and b is 45
public class ExampleOverloading{
public static void main(String[] args) {
int a = 11;
int b = 6;
double c = 7.3;
double d = 9.4;
int result1 = minFunction(a, b);
// same function name with different parameters
double result2 = minFunction(c, d);
System.out.println("Minimum Value = " + result1);
System.out.println("Minimum Value = " + result2);
}
// for integer
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
// for double
public static double minFunction(double n1, double n2) {
double min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
}
Minimum Value = 6 Minimum Value = 7.3
public class CommandLine {
public static void main(String args[]){
for(int i=0; i<args.length; i++){
System.out.println("args[" + i + "]: " +
args[i]);
}
}
}
java CommandLine this is a command line 200 -100
args[0]: this args[1]: is args[2]: a args[3]: command args[4]: line args[5]: 200 args[6]: -100
// A simple constructor.
class MyClass {
int x;
// Following is the constructor
MyClass() {
x = 10;
}
}
public class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass();
MyClass t2 = new MyClass();
System.out.println(t1.x + " " + t2.x);
}
}
// A simple constructor.
class MyClass {
int x;
// Following is the constructor
MyClass(int i ) {
x = i;
}
}
public class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass( 10 );
MyClass t2 = new MyClass( 20 );
System.out.println(t1.x + " " + t2.x);
}
}
10 20
typeName... parameterName
public class VarargsDemo {
public static void main(String args[]) {
// Call method with variable args
printMax(34, 3, 3, 2, 56.5);
printMax(new double[]{1, 2, 3});
}
public static void printMax( double... numbers) {
if (numbers.length == 0) {
System.out.println("No argument passed");
return;
}
double result = numbers[0];
for (int i = 1; i < numbers.length; i++)
if (numbers[i] > result)
result = numbers[i];
System.out.println("The max value is " + result);
}
}
The max value is 56.5 The max value is 3.0
protected void finalize( )
{
// finalization code here
}
public static List<T> find(Class<T> clazz,String userId){
....
}
public static <T extends CommonService> T getService(Class<T> clazz) {
T service = (T) serviceMap.get(clazz.getName());
if (service == null) {
service = (T) ServiceLocator.getService(clazz.getName());
serviceMap.put(clazz.getName(), service);
}
return service;
}
public static <T> T getService(Class<? extends CommonService> clazz) {
T service = (T) serviceMap.get(clazz.getName());
if (service == null) {
service = (T) ServiceLocator.getService(clazz.getName());
serviceMap.put(clazz.getName(), service);
}
return service;
}
public abstract class CommonService {
private static HashMap<String, CommonService> serviceMap = new HashMap<String, CommonService>();
//这里是泛型方法定义
.
.
.
}
public class Main {
public static void main(String[] args) {
NoticeService noticeService=CommonService.getService(NoticeService.class);//正确的使用第一种泛型方法,不会出现编译错误。
NoticeService noticeService=CommonService.getService(UserService.class);//不正确的使用第一种泛型方法,会出现编译错误。
}
}
public class Main {
public static void main(String[] args) {
NoticeService noticeService=CommonService.getService(NoticeService.class);//正确的使用第二种泛型方法,不会出现编译错误,逻辑也正确,运行时不会出现异常。
NoticeService noticeService=CommonService.getService(UserService.class);//不正确的使用第二种泛型方法,不会出现编译错误,但逻辑不正确,运行时会出现异常,危险!
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有