小编Muh*_*edy的帖子

java类驻留在内存中的时间

我正在考虑一个类驻留在内存中的多少,以防内存中的其他对象没有访问它?

例如,假设我有一些这样的类:

public class OrderNumber {
 private static long counter = 0;

 public static long getOrderNumber(){
          if (counter >= 100) {
            return counter = 1;
          }
          return ++counter;
 }
}
Run Code Online (Sandbox Code Playgroud)

我从另一个类调用它的静态方法:

long number = OrderNumber.getOrderNumber(); 
Run Code Online (Sandbox Code Playgroud)

每次我调用它,它都会返回一个增量数字,1,2,3,4 ......

所以,我的问题是这个方法返回初始值的概率是多少,它应该返回序列值?

java classloader

2
推荐指数
1
解决办法
271
查看次数

if(str1 == null)抛出NullPointerException时

在java中,以下行是否有可能(甚至0.01%)抛出NullPointerException?

public static void handleRequest(String str1){
   if (str1 == null){  // this line throws NPE, how come !! is it a JDK1.5 bug!!
        return null;
   }
   // other staff
}
Run Code Online (Sandbox Code Playgroud)

实际上我在代码中掉了一些bug,它说方法中的exact上面一行会抛出一个java.lang.NullPointerException?!

java string nullpointerexception

2
推荐指数
2
解决办法
1179
查看次数

无法针对xsd架构验证xml doc(无法找到元素'replyMessage'的声明)

我使用以下代码来验证针对XSD架构的XML文件

package com.forat.xsd;

import java.io.IOException;
import java.net.URL;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class XSDValidate {

 public void validate(String xmlFile, String xsd_url) {
  try {
   SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
   Schema schema = factory.newSchema(new URL(xsd_url));
   Validator validator = schema.newValidator();
   ValidationHandler handler = new ValidationHandler();
   validator.setErrorHandler(handler);
   validator.validate(getSource(xmlFile));

   if (handler.errorsFound == true) {
    System.err.println("Validation Error : "+ handler.exception.getMessage());
   }else {
    System.out.println("DONE");
   }
  } catch (SAXException e) {
   e.printStackTrace();
  } catch …
Run Code Online (Sandbox Code Playgroud)

java schema xsd jaxp

2
推荐指数
1
解决办法
6672
查看次数

GWT:当鼠标熄灭时,让PopupPanel消失

这是GWT PopupPanelShowCase:http://gwt.google.com/samples/Showcase/Showcase.html#!CwBasicPopup

当用户点击面板时,Panel隐藏了..

我需要的是当用户将鼠标移出面板区域时隐藏它...

怎么做到这一点?

gwt panel popup

2
推荐指数
1
解决办法
4912
查看次数

Objective-c运行时是指比直接c/c ++程序更多的层吗?

我已经读过,objective-c程序需要objective-c运行时才能运行.

AFAIK,两个C/C++程序都不需要运行任何运行时环境.因为生成的二进制代码正由底层OS直接执行.

那么这意味着Objective-c程序需要一个冗余层来运行,正确吗?如果是这样,这个层看起来像Java VM和.net运行时还是看起来像Qt运行时(在一些额外的库中)?

EDIT: 一些读出后,我发现,在objc编译器生成在生成的编译代码一些更多的信息,其负责的许多东西,如方法通过(objc_sendMsg(),introspection及其他)

谢谢.

runtime objective-c objective-c-runtime

2
推荐指数
1
解决办法
491
查看次数

realloc只会扩展内存还是会导致内存问题?

我有以下代码:

#include <stdio.h>
#include <stdlib.h>
#define OUT

void getDataFromServer(OUT int** array, OUT int* size)
{
    static int tmpArr[] = {0x00, 0x01, 0x02, 0x03,  0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
                        0x10, 0x11, 0x12, 0x13,  0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
    *size = sizeof tmpArr / sizeof(int);
    printf("Before realloc: %p\n", *array);
    *array = realloc(*array, sizeof(*array) * *size);
    printf("After realloc : %p\n", *array);
    int i=0;
    for (; i < *size; i++) …
Run Code Online (Sandbox Code Playgroud)

c memory-management realloc

2
推荐指数
1
解决办法
1079
查看次数

检查Null Wrappers是否符合原始值

Integer i = null;
if (i == 3)
Run Code Online (Sandbox Code Playgroud)

为什么上面的第二行抛出一个NullPointerException,恕我直言,这只有一个含义,即Wrapper Object i将被取消装箱,产生异常,例如:

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(null);
int x = list.get(0);
Run Code Online (Sandbox Code Playgroud)

编辑:你能为我提供一些格式的文件吗?

java boxing expression wrapper

2
推荐指数
1
解决办法
3896
查看次数

更改rightBarButtonItem的起始轴

我在rightBarButtonItem上有以下自定义按钮

UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,105,30);
[button1 setBackgroundImage:[UIImage imageNamed: @"image1.png"] forState:UIControlStateNormal];
[button1 addTarget:appDelegate action:@selector(Open_Link1) forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
[button1 release];
Run Code Online (Sandbox Code Playgroud)

我需要将rightBarButtonItem向上移动2 px ...

我试过设置它但不起作用!

iphone uibarbuttonitem

2
推荐指数
1
解决办法
3338
查看次数

在迭代集合时检查是否为空

哪个更好?

List list = ... //get list from somewhere

for (int i=0; list != null && i < list.size(); i++){
    // ...
}
Run Code Online (Sandbox Code Playgroud)

要么?

List list = ... //get list from somewhere


if (list != null){
    for (int i = 0; i < list.size(); i++){
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

我从这里得到了这个想法:Scala还是Java?探索神话和事实

java coding-style

2
推荐指数
1
解决办法
72
查看次数

数组排序与parallelSort性能比较

我已经下载了java8-ea发行版并在Array.sort和之间进行了快速比较Arrays.parallelSort.

这就是结果: 在此输入图像描述

我可以理解,praralleSort至少应该像普通老式机器一样sort,如果不是更快......但事实并非如此.

对以下规格进行了比较:

惠普ProBook Intel Core i54G RAMUbuntu 13.04 Linux同版本的JDK:Java HotSpot(TM) 64-Bit Server VM (build 25.0-b23, mixed mode)

我通过这种方式创建了一个包含三个字段的Custom对象数组(以保留顺序添加对象):

package com.cmd;

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {

        for (int i=100; i <= 10_000_000; i*=10){
            runTest(i);
        }
    }

    private static void runTest(final int size){

        // Fist obtain two Arrays of same data
        Employee[] empArrForSort = createVeryLargeEmpArray(size);
        Employee[] empArrForSortCopy = Arrays.copyOf(empArrForSort, …
Run Code Online (Sandbox Code Playgroud)

java parallel-processing java-8

2
推荐指数
1
解决办法
1536
查看次数